request for help on lex yacc coding / compiling
Hello i have been coding with afew class mates a certain codes that is supposed to compile lex and yacc both and then run them together but we get a bunch of errors can someone help us see whats wrong?
LEX: %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 "^" AND "&" 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}*{grea ter}*{lesser}*{greaterequal}*{lesserequal}*{minus}*{plus}*{not}*{notequal}*{or}*{multiply}*{AND}*{po wer}*{doubleQuotes} comment {backslash}{parcent}{space}*({letters}*{space}*{identifier}*{space}*{decimal}*{space}*{hex}*{space}* {octal}*{space}*{binaryInt}*{space}*{dbland}*{devide}*{assign}*{equal}*{greater}*{lesser}*{greatereq ual}*{lesserequal}*{minus}*{p$us}*{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; {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: %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 AND 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 AND 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> #define YYDEBUG 1 int yylex(void); void yyerror(const char *); tyepdef struct node{char * token; struct node *left; struct node *right; }node; 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 at line %d\n", s, yylineno); return; } ERRORS : test.y:60: error: expected identifier or ‘(’ before ‘%’ token test.y:64:1: warning: "YYDEBUG" redefined y.tab.c:197:1: warning: this is the location of the previous definition In file included from test.y:75: lex.yy.c:157: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token lex.yy.c:203: error: expected specifier-qualifier-list before ‘FILE’ lex.yy.c:299: error: expected ‘)’ before ‘*’ token lex.yy.c:301: error: expected ‘)’ before ‘*’ token lex.yy.c:309: error: expected declaration specifiers or ‘...’ before ‘FILE’ lex.yy.c:349: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token In file included from test.y:75: lex.yy.c:951: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token lex.yy.c:953: error: expected ‘)’ before ‘*’ token lex.yy.c:955: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token lex.yy.c:957: error: expected ‘)’ before ‘*’ token In file included from test.y:75: lex.yy.c: In function ‘yylex’: lex.yy.c:1117: error: ‘yyin’ undeclared (first use in this function) lex.yy.c:1117: error: (Each undeclared identifier is reported only once lex.yy.c:1117: error: for each function it appears in.) lex.yy.c:1118: error: ‘stdin’ undeclared (first use in this function) lex.yy.c:1120: error: ‘yyout’ undeclared (first use in this function) lex.yy.c:1121: error: ‘stdout’ undeclared (first use in this function) In file included from test.y:75: test.l:74: error: ‘assign’ undeclared (first use in this function) In file included from test.y:75: test.l:94: error: ‘ltrlString’ undeclared (first use in this function) test.l:95: error: expected ‘;’ before ‘break’ In file included from test.y:75: test.l:111: warning: incompatible implicit declaration of built-in function ‘fwrite’ In file included from test.y:75: lex.yy.c:1411: error: ‘struct yy_buffer_state’ has no member named ‘yy_buffer_status’ lex.yy.c:1422: error: ‘struct yy_buffer_state’ has no member named ‘yy_n_chars’ lex.yy.c:1423: error: ‘struct yy_buffer_state’ has no member named ‘yy_input_file’ lex.yy.c:1424: error: ‘struct yy_buffer_state’ has no member named ‘yy_buffer_status’ lex.yy.c:1434: error: ‘struct yy_buffer_state’ has no member named ‘yy_ch_buf’ lex.yy.c:1513: error: ‘struct yy_buffer_state’ has no member named ‘yy_ch_buf’ lex.yy.c: In function ‘yy_get_next_buffer’: lex.yy.c:1540: error: ‘struct yy_buffer_state’ has no member named ‘yy_ch_buf’ lex.yy.c:1545: error: ‘struct yy_buffer_state’ has no member named ‘yy_ch_buf’ lex.yy.c:1549: error: ‘struct yy_buffer_state’ has no member named ‘yy_fill_buffer’ lex.yy.c:1576: error: ‘struct yy_buffer_state’ has no member named ‘yy_buffer_status’ lex.yy.c:1580: error: ‘struct yy_buffer_state’ has no member named ‘yy_n_chars’ lex.yy.c:1585: error: ‘struct yy_buffer_state’ has no member named ‘yy_buf_size’ lex.yy.c:1594: error: ‘struct yy_buffer_state’ has no member named ‘yy_ch_buf’ lex.yy.c:1596: error: ‘struct yy_buffer_state’ has no member named ‘yy_is_our_buffer’ lex.yy.c:1598: error: ‘struct yy_buffer_state’ has no member named ‘yy_buf_size’ lex.yy.c:1601: error: ‘struct yy_buffer_state’ has no member named ‘yy_buf_size’ lex.yy.c:1601: error: ‘struct yy_buffer_state’ has no member named ‘yy_buf_size’ lex.yy.c:1603: error: ‘struct yy_buffer_state’ has no member named ‘yy_buf_size’ lex.yy.c:1605: error: ‘struct yy_buffer_state’ has no member named ‘yy_ch_buf’ lex.yy.c:1607: error: ‘struct yy_buffer_state’ has no member named ‘yy_ch_buf’ lex.yy.c:1607: error: ‘struct yy_buffer_state’ has no member named ‘yy_buf_size’ lex.yy.c:1611: error: ‘struct yy_buffer_state’ has no member named ‘yy_ch_buf’ lex.yy.c:1613: error: ‘struct yy_buffer_state’ has no member named ‘yy_ch_buf’ lex.yy.c:1617: error: ‘struct yy_buffer_state’ has no member named ‘yy_ch_buf’ lex.yy.c:1619: error: ‘struct yy_buffer_state’ has no member named ‘yy_buf_size’ lex.yy.c:1628: error: ‘struct yy_buffer_state’ has no member named ‘yy_is_interactive’ lex.yy.c:1628: error: ‘yyin’ undeclared (first use in this function) lex.yy.c:1628: error: ‘struct yy_buffer_state’ has no member named ‘yy_ch_buf’ lex.yy.c:1628: error: ‘struct yy_buffer_state’ has no member named ‘yy_ch_buf’ lex.yy.c:1628: error: ‘struct yy_buffer_state’ has no member named ‘yy_ch_buf’ lex.yy.c:1631: error: ‘struct yy_buffer_state’ has no member named ‘yy_n_chars’ lex.yy.c:1645: error: ‘struct yy_buffer_state’ has no member named ‘yy_buffer_status’ lex.yy.c:1653: error: ‘struct yy_buffer_state’ has no member named ‘yy_buf_size’ lex.yy.c:1656: error: ‘struct yy_buffer_state’ has no member named ‘yy_ch_buf’ lex.yy.c:1656: error: ‘struct yy_buffer_state’ has no member named ‘yy_ch_buf’ lex.yy.c:1657: error: ‘struct yy_buffer_state’ has no member named ‘yy_ch_buf’ lex.yy.c:1662: error: ‘struct yy_buffer_state’ has no member named ‘yy_ch_buf’ lex.yy.c:1663: error: ‘struct yy_buffer_state’ has no member named ‘yy_ch_buf’ lex.yy.c:1665: error: ‘struct yy_buffer_state’ has no member named ‘yy_ch_buf’ lex.yy.c: In function ‘yyunput’: lex.yy.c:1736: error: ‘struct yy_buffer_state’ has no member named ‘yy_ch_buf’ lex.yy.c:1740: error: ‘struct yy_buffer_state’ has no member named ‘yy_ch_buf’ lex.yy.c:1741: error: ‘struct yy_buffer_state’ has no member named ‘yy_buf_size’ lex.yy.c:1743: error: ‘struct yy_buffer_state’ has no member named ‘yy_ch_buf’ lex.yy.c:1745: error: ‘struct yy_buffer_state’ has no member named ‘yy_ch_buf’ lex.yy.c:1750: error: ‘struct yy_buffer_state’ has no member named ‘yy_n_chars’ lex.yy.c:1751: error: ‘struct yy_buffer_state’ has no member named ‘yy_buf_size’ lex.yy.c:1753: error: ‘struct yy_buffer_state’ has no member named ‘yy_ch_buf’ lex.yy.c: In function ‘input’: lex.yy.c:1786: error: ‘struct yy_buffer_state’ has no member named ‘yy_ch_buf’ lex.yy.c:1809: error: ‘yyin’ undeclared (first use in this function) lex.yy.c: At top level: lex.yy.c:1852: error: expected ‘)’ before ‘*’ token lex.yy.c: In function ‘yy_switch_to_buffer’: lex.yy.c:1885: error: ‘struct yy_buffer_state’ has no member named ‘yy_buf_pos’ lex.yy.c:1886: error: ‘struct yy_buffer_state’ has no member named ‘yy_n_chars’ lex.yy.c: In function ‘yy_load_buffer_state’: lex.yy.c:1902: error: ‘struct yy_buffer_state’ has no member named ‘yy_n_chars’ lex.yy.c:1903: error: ‘struct yy_buffer_state’ has no member named ‘yy_buf_pos’ lex.yy.c:1904: error: ‘yyin’ undeclared (first use in this function) lex.yy.c:1904: error: ‘struct yy_buffer_state’ has no member named ‘yy_input_file’ lex.yy.c: At top level: lex.yy.c:1914: error: expected ‘)’ before ‘*’ token lex.yy.c: In function ‘yy_delete_buffer’: lex.yy.c:1951: error: ‘struct yy_buffer_state’ has no member named ‘yy_is_our_buffer’ lex.yy.c:1952: error: ‘struct yy_buffer_state’ has no member named ‘yy_ch_buf’ lex.yy.c: At top level: lex.yy.c:1965: error: expected declaration specifiers or ‘...’ before ‘FILE’ lex.yy.c: In function ‘yy_init_buffer’: lex.yy.c:1972: error: ‘struct yy_buffer_state’ has no member named ‘yy_input_file’ lex.yy.c:1972: error: ‘file’ undeclared (first use in this function) lex.yy.c:1973: error: ‘struct yy_buffer_state’ has no member named ‘yy_fill_buffer’ lex.yy.c:1980: error: ‘struct yy_buffer_state’ has no member named ‘yy_bs_lineno’ lex.yy.c:1981: error: ‘struct yy_buffer_state’ has no member named ‘yy_bs_column’ lex.yy.c:1984: error: ‘struct yy_buffer_state’ has no member named ‘yy_is_interactive’ lex.yy.c: In function ‘yy_flush_buffer’: lex.yy.c:1998: error: ‘struct yy_buffer_state’ has no member named ‘yy_n_chars’ lex.yy.c:2004: error: ‘struct yy_buffer_state’ has no member named ‘yy_ch_buf’ lex.yy.c:2005: error: ‘struct yy_buffer_state’ has no member named ‘yy_ch_buf’ lex.yy.c:2007: error: ‘struct yy_buffer_state’ has no member named ‘yy_buf_pos’ lex.yy.c:2007: error: ‘struct yy_buffer_state’ has no member named ‘yy_ch_buf’ lex.yy.c:2009: error: ‘struct yy_buffer_state’ has no member named ‘yy_at_bol’ lex.yy.c:2010: error: ‘struct yy_buffer_state’ has no member named ‘yy_buffer_status’ lex.yy.c: In function ‘yypush_buffer_state’: lex.yy.c:2034: error: ‘struct yy_buffer_state’ has no member named ‘yy_buf_pos’ lex.yy.c:2035: error: ‘struct yy_buffer_state’ has no member named ‘yy_n_chars’ lex.yy.c: In function ‘yy_scan_buffer’: lex.yy.c:2134: error: ‘struct yy_buffer_state’ has no member named ‘yy_buf_size’ lex.yy.c:2135: error: ‘struct yy_buffer_state’ has no member named ‘yy_buf_pos’ lex.yy.c:2135: error: ‘struct yy_buffer_state’ has no member named ‘yy_ch_buf’ lex.yy.c:2136: error: ‘struct yy_buffer_state’ has no member named ‘yy_is_our_buffer’ lex.yy.c:2137: error: ‘struct yy_buffer_state’ has no member named ‘yy_input_file’ lex.yy.c:2138: error: ‘struct yy_buffer_state’ has no member named ‘yy_n_chars’ lex.yy.c:2138: error: ‘struct yy_buffer_state’ has no member named ‘yy_buf_size’ lex.yy.c:2139: error: ‘struct yy_buffer_state’ has no member named ‘yy_is_interactive’ lex.yy.c:2140: error: ‘struct yy_buffer_state’ has no member named ‘yy_at_bol’ lex.yy.c:2141: error: ‘struct yy_buffer_state’ has no member named ‘yy_fill_buffer’ lex.yy.c:2142: error: ‘struct yy_buffer_state’ has no member named ‘yy_buffer_status’ lex.yy.c: In function ‘yy_scan_bytes’: lex.yy.c:2195: error: ‘struct yy_buffer_state’ has no member named ‘yy_is_our_buffer’ lex.yy.c: In function ‘yy_fatal_error’: lex.yy.c:2206: warning: incompatible implicit declaration of built-in function ‘fprintf’ lex.yy.c:2206: error: ‘stderr’ undeclared (first use in this function) lex.yy.c: At top level: lex.yy.c:2241: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token lex.yy.c:2249: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token lex.yy.c:2287: error: expected ‘)’ before ‘*’ token lex.yy.c:2292: error: expected ‘)’ before ‘*’ token lex.yy.c: In function ‘yy_init_globals’: lex.yy.c:2328: error: ‘yyin’ undeclared (first use in this function) lex.yy.c:2328: error: ‘FILE’ undeclared (first use in this function) lex.yy.c:2328: error: expected expression before ‘)’ token lex.yy.c:2329: error: ‘yyout’ undeclared (first use in this function) lex.yy.c:2329: error: expected expression before ‘)’ token test.y: At top level: test.y:78: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token test.y:86: error: expected identifier or ‘(’ before ‘return’ test.y:87: error: expected ‘)’ before ‘*’ token test.y: In function ‘yyerror’: test.y:96: warning: incompatible implicit declaration of built-in function ‘fprintf’ test.y:96: error: ‘stderr’ undeclared (first use in this function) y.tab.c: In function ‘yyparse’: y.tab.c:411: error: ‘yyname’ undeclared (first use in this function) y.tab.c:413: warning: incompatible implicit declaration of built-in function ‘printf’ y.tab.c:423: warning: incompatible implicit declaration of built-in function ‘printf’ y.tab.c:464: warning: incompatible implicit declaration of built-in function ‘printf’ y.tab.c:479: warning: incompatible implicit declaration of built-in function ‘printf’ y.tab.c:497: warning: incompatible implicit declaration of built-in function ‘printf’ y.tab.c:508: warning: incompatible implicit declaration of built-in function ‘printf’ y.tab.c:509: error: ‘yyrule’ undeclared (first use in this function) 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:53: error: expected ‘;’ before ‘}’ token test.y:55: error: expected ‘;’ before ‘}’ token |
Welcome to LQ!
It is difficult for other members to read code posted as a "wall of text", which can discourage others from trying to understand your problems and provide useful replies. Please edit your post using [code]...[/code] tags for better formatting and readability of code and error sections. You may type those yourself or click the "#" button in the advanced edit controls. It is also important to provide the context of those error messages, that is, the exact commands and applications which produced them. Are you actually using lex and yacc, or flex and bison? Make and makefile or shell commands? It can also be important to know the versions of each application and the platform that you are compiling on. Everyone here volunteers their time, so you can help us to help you by making it easier for us to quickly understand what is happening. I have tried to understnd and compile your code but I receive a completely different set of errors. The most relevant I see is this: Code:
test.lex:91: missing quote I also tried to correlate your posted parser errors with your code but could make no sense of it without the original formatting of your code, and the compile time context. So please try to restate your question as specifically as possible and use code tags for formatting and readability. |
Also, since I see reference to "your class-mates," please take a cue from "this old sometimes community-college instructor" as I implore you to "take this to your instructor!"
Your instructor, being the one who created this exercise and then gave it to you, can give you a nudge in the right direction. (Which, despite the number of error-messages it has produced, is actually pretty straightforward to fix. But, I digress.) My most-important point is that, by taking your problems to this particular person, you thereby inform him-or-her that certain students are having this problem. S/He can therefore incorporate this into an upcoming class session, thereby benefiting other students (altogether unknown to you ...) who might be having the same problem. Also, s/he will know that parts of his/her earlier class presentations might need to be re-visited. (Successful ...) Education is very much a two-way street. Instructors need for students to complete that feedback-loop, instead of concealing their struggles from the instructor's view. Although it is quite probable that your instructor does :eek: monitor LQ, you and your classmates need to cross that bridge directly ... and, by the way, "without embarrassment." You're not "admitting defeat." You're availing yourself of the most-knowledgeable resource at your disposal. (Which, by the way, is something that you should be doing constantly "on the job," even though too-many people don't do this, to the eternal vexation of their co-workers ... but once again I digress.) :rolleyes: By choosing this resource (versus LQ ...), you're improving the quality and therefore the outcome of the class that you are presently taking. |
@OP: Also please learn this by heart: http://www.catb.org/~esr/faqs/smart-questions.html
|
All times are GMT -5. The time now is 12:59 PM. |