16.05.2015 Views

Working with the Unix OS

Working with the Unix OS

Working with the Unix OS

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Development Tools<br />

*/<br />

%{<br />

#define YYSTYPE double /* data type for yacc stack */<br />

#include <br />

#include <br />

char *progname;<br />

int lineno=1;<br />

%}<br />

%token NUMBER<br />

%left '+' '-' /* left associative, same precedence */<br />

%left '*' '/' /* left assoc., higher precedence */<br />

%%<br />

list: /* nothing */<br />

| list '\n'<br />

| list expr '\n' { printf("\t%.8g\n", $2);}<br />

;<br />

expr:<br />

NUMBER<br />

| expr '+' expr {$$ =$1 + $3;}<br />

| expr '-' expr {$$ =$1 - $3;}<br />

| expr '*' expr {$$ =$1 * $3;}<br />

| expr '/' expr {$$ =$1 / $3;}<br />

| '(' expr ')' {$$ =$2;}<br />

;<br />

%% /*end of grammar */<br />

main(int argc, char *argv[] )<br />

{<br />

progname = argv[0];<br />

yyparse();<br />

}<br />

yylex()<br />

{<br />

int c;<br />

while ((c=getchar() == ' ' || c == '\t');<br />

}<br />

if (c == EOF)<br />

return 0;<br />

if (c == '.' || isdigit(c))<br />

{<br />

ungetc(c, stdin);<br />

scanf("%1f", &&yylval);<br />

return NUMBER;<br />

}<br />

if (c == '\n')<br />

lineno++;<br />

return c;<br />

yyerror(char *s)<br />

{<br />

warning(s, (char *) 0);<br />

}<br />

warning(char *s, char *t)<br />

{<br />

fprintf(stderr, "%s: %s", progname, s);<br />

if (t)<br />

fprintf(stderr, " %s", t);<br />

fprintf(stderr, "near line $d\n", lineno);<br />

62

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!