Lex Yacc parser compiling get few errors tried searching solution cant find -
we have task compile lex , yacc praser code run them using cc tab.y.c -ll -ly command when each apart compile fine compile both parts 1 gives 10 lines of errors.
first part lex code:
%option yylineno %pointer %{ #include <stdlib.h> #include <string.h> void yyerror(const char *); %} low \_ identifier {letters}{digit}*{low}{letters}|{letters} stringerr {doublequotes}{doublequotes}+|{doublequotes} charerr {singlequotes}+{digits}*{letters}*{singlequotes}+ err {charerr}|{stringerr} type boolean|string|char|integer|intptr|charptr|var dbland "&&" devide "/" assign "=" equal "==" greater ">" lesser "<" greaterequal ">=" lesserequal "<=" minus "-" plus "+" not "!" notequal "!=" or "||" multiply "*" power "^" , "&" literbool true|false letter [a-z]|[a-z] letters {letter}+ singlequotes ' literchar {singlequotes}{letter}{singlequotes} digit [0-9] digitzero 0 octaldigit [1-7] octal {digitzero}{octaldigit}{digitzero}*{octaldigit}* digits {digit}+ digitnozero[1-9] decimal {digit}|{digitnozero}{digits} hexletter a|b|c|d|e|f hex 0(x|x){digit}+{hexletter}*|0(x|x){digit}*{hexletter}+ letterb b digitone 1 binaryint ({digitzero}|{digitone})+{letterb} integer {binaryint}|{hex}|{octal}|{decimal} doublequotes \" ltrlstring {doublequotes}{letters}*{decimal}*{hex}*{octal}*{binaryint}*{dbland}*{devide}*{assign}*{equal}*{greater}*{lesser}*{greaterequal}*{lesserequal}*{minus}*{plus}*{not}*{notequal}*{or}*{multiply}*{and}*{power}*{doublequotes} comment {backslash}{parcent}{space}*({letters}*{space}*{identifier}*{space}*{decimal}*{space}*{hex}*{space}*{octal}*{space}*{binaryint}*{space}*{dbland}*{devide}*{assign}*{equal}*{greater}*{lesser}*{greaterequal}*{lesserequal}*{minus}*{$nus}*{plus}*{not}*{notequal}*{or}*{multiply}*{and}*{power}*{ltrlstring}*)*{space}{parcent}{backslash} colon ":" opensq "[" closesq "]" semicolon ";" parcent "%" space " " comma "," backslash "/" clos ")" opn "(" charptr charptr pointer {colon}{space}{charptr}|"="{space}"&"{identifier} pointererr "&"{identifier}|{charptr} else "else"{space}* statif "if"{space}* whileloop "while"{space}* returnstate "return"{space}* func "procedure"{space}* %% {dbland} return dbland; {devide} return devide; {assign} return assign; {equal} return equal; {greater} return greater; {lesser} return lesser; {greaterequal} return greaterequal; {lesserequal} return lesserequal; {minus} return minus; {plus} return plus; {not} return not; {notequal} return notequal; {or} return or; {multiply} return multiply; {power} return power; {and} return and; {literbool} return literbool; {literchar} return literchar; {decimal} return decimal; {hex} return hex; {octal} return octal; {binaryint} return binaryint; {ltrlstring} return ltrlstring {type} return type; {identifier} return identifier; {err} return err; {comment} return comment; {pointer} return pointer; {pointererr} return pointererr; {statif} return statif; {else} return else; {whileloop} return whileloop; {returnstate} return returnstate; {func} return func; {semicolon} return semicolon; {comma} return comma; [\*\(\)\.\+\-\%] { return *yytext; } [0-9][0-9]* { return 'n'; } [ \t\n] ; /* skip whitespace */ %% int yywrap(void) { return 1; }
yacc code:
%token low identifier stringerr charerr err type operator literbool letter %token dbland literchar decimal hex octal integer %token binaryint ltrstring comment pointer pointererr %token statif else whileloop returnstate func comma semicolon %token eol lparen rparen uminus %left equal greater notequal lesser greaterequal lesserequal %left '|' %left '&' %left shift /* << >> */ %left minus plus %left multiply devide '%' mod %left power %left not or , comma %nonassoc uminus %% s: block; block: expr|logicop|varible_declaration|ifexp|whileexp|procexp|semicolon; expr: exp{printtree($1);} exp: identifier {$$=mknode(yytext,null,null);} | lparen expr rparen {$$=$2;} | exp plus exp {$$= mknode("+" $1,$3);} | exp minus exp {$$= mknode("-" $1, $3);} | exp multiply exp {$$=mknode("*" $1, $3);} | exp devide exp {$$=mknode("/" $1, $3);} | "-" exp %prec uminus {-$2} varible_declaration: var{printtree($1);} var : "var" identifier_list ":" typet ";" {$$ = mknode("var", $2, $4);} typet: integer{$$ = mknode(yytext,null,null);} |binaryint {$$ = mknode(yytext,null,null);} |type {$$ = mknode(yytext,null,null);} identifier_list: identifier_list comma identifier_list {$$= mknode(",",$1, $3);} |identifier {$$ = mknode(yytext,null,null);} logicop: op{printtree($1);} op:exp equal exp {$$ = mknode("==",$1,$3);} |exp notequal exp {$$ = mknode("!=",$1,$3);} |exp or exp {$$ = mknode("||",$1,$3);} |exp , exp {$$ = mknode("&&",$1,$3);} |exp greater exp {$$ = mknode(">",$1,$3);} |exp greaterequal exp {$$ = mknode(">=",$1,$3);} |exp lesser exp {$$ = mknode("<",$1,$3);} |exp lesserequal exp {$$ = mknode("<=",$1,$3);} ifexp: if{printtree($1);} if:statif '(' logicop ')' '{' block '}' else '{' block '}' {$$ = mknode("if",$3,mknode("else",$6,$10));} |statif '(' logicop ')' '{' block '}' {$$=mknode("if",$3,$6);} whileexp: while{printtree($1)} while:whileloop '(' logicop ')' '{' block '}' {$$=mknode("while",$3,$6);} procexp: proc{printtree($1)} proc:func identifier '(' identifier_list ')' returnstate type '{' block '}' {$$ = mknode("procedure",$2,"todo");} %% #include <stdio.h> #include <stdlib.h> #include <string.h> int yylex(void); void yyerror(const char *); tyepdef struct node{ char * token; struct node *left; struct node *right; }; node * mknode(char * token , node * left,node * right); void printtree(node * tree); #define yystype struct node * #include "lex.yy.c" main() { return yyparse(); } nose * mknode(char * token,node * left, node * right) { node * newnode = (node*)malloc(sizeof(node)); char 8 newstr = (char*)malloc(sizeof(token)+1); strcpy("newstr,token"); newnode->left=left; newnode->right=right; newnode->token=newstr; }return newnode; void printtree(node * tree) { printf("%s\n",tree->token); if (tree->left) printtree(tree->left); if (tree->right) printtree(tree->left); } extern int yylineno; void yyerror(const char *s) { fprintf(stderr, "%s @ line %d\n", s, yylineno); return; }
the errors following:
[tzurisa@ac-aix backup]$ nano test.l [tzurisa@ac-aix backup]$ lex test.l [tzurisa@ac-aix backup]$ yacc test.y [tzurisa@ac-aix backup]$ cc -o test y.tab.c -ll -ly test.y:63: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘struct’ test.y:68: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token test.y:69: error: expected ‘)’ before ‘*’ token in file included test.y:72: test.l: in function ‘yylex’: test.l:74: error: ‘assign’ undeclared (first use in function) test.l:74: error: (each undeclared identifier reported once test.l:74: error: each function appears in.) in file included test.y:72: test.l:94: error: ‘ltrlstring’ undeclared (first use in function) test.l:95: error: expected ‘;’ before ‘break’ test.y: @ top level: test.y:75: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token test.y:83: error: expected identifier or ‘(’ before ‘return’ test.y:84: error: expected ‘)’ before ‘*’ token test.y: in function ‘yyparse’: test.y:20: error: expected ‘)’ before ‘yyvsp’ test.y:21: error: expected ‘)’ before ‘yyvsp’ test.y:22: error: expected ‘)’ before ‘yyvsp’ test.y:23: error: expected ‘)’ before ‘yyvsp’ test.y:24: error: expected ‘;’ before ‘}’ token test.y:51: error: expected ‘;’ before ‘}’ token test.y:53: error: expected ‘;’ before ‘}’ token
will appriciate of can tell whats wrong here have tried many things still these errors ..
if @ line 63 of test.y
, indicated in first error message, see first problem; misspelled typedef
. fix that, , check remaining errors, if any, looking @ indicated lines.
Comments
Post a Comment