• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Newbie problems with Yacc

suszterpatt

Senior member
As much as I hate asking for help with homework, I feel it's justified in the light that nobody ever bothered to teach us a single word about Flex or Yacc. As such, I'm completely in the dark, and I'm going on whatever tutorials I can scrape together from the internet.

I'm supposed to write a lexer and syntax checker for a simple, self-made programming language. I started with Yacc's input file, pasted at the end of the post (code tags aren't working :\).

When I run yacc (bison to be exact), it says "start symbol program does not derive a sentence", and proceeds to tell me that all my nonterminals and rules are useless. Being a complete beginner, I'm stuck.

Any help is appreciated: the more the merrier, since I don't really have any idea of what I'm supposed to do and why what I'm doing is good or bad.

Thanks.



%token VALUE VAR IF ELSIF ELSE ENDIF CASE OF DEFAULT ENDCASE FOR TO BY ENDFOR WHILE ENDWHILE PRINT
%left EQ NE '<' '>' LE GE
%left '+' '-'
%left '*' '/' '%'
%{
#include <stdio.h>
void yyerror(char *);
int yylex(void);
int sym[260];
%}
%%
program:
program stment '\n' { printf("%d\n", $1); }
;
stment:
assign
| if
| case
| floop
| wloop
| print
|
;
assign:
VAR '=' expr ';' { }
;
if:
IF cond program stment ifend { }
;
ifend:
ENDIF
| ELSIF cond program stment ifend
| ELSE program stment ENDIF
;
case:
CASE expr OF VALUE ':' program stment casend
;
casend:
ENDCASE
| OF VALUE ':' program stment casend
| DEFAULT ':' program stment ENDCASE
;
floop:
fromto step program stment ENDFOR
;
fromto:
FOR VAR '=' VALUE TO VALUE
;
step:
BY VALUE
|
;
wloop:
WHILE cond program stment ENDWHILE
;
print:
PRINT expr
;
expr:
liter
| expr '+' expr
| expr '-' expr
| expr '*' expr
| expr '/' expr
| expr '%' expr
| '(' expr ')'
;
cond:
expr EQ expr
| expr NE expr
| expr '<' expr
| expr '>' expr
| expr LE expr
| expr GE expr
;
liter:
VAR
| VALUE
;
%%
void yyerror(char *s) {
fprintf(stderr, "%s\n", s);
return 0;
}
int main(void) {
yyparse();
return 0;
}
 
Thats a bloody bummer. I learned everything I know about yacc and bison (I know VERY little about them) from this tutorial.

Offhand, I don't notice any C-language directives after your grammar rules, but it could just be the lousy forum formatting.
 
I actually found the solution: I messed up the grammar.

program:
program stment '\n' { printf("%d\n", $1); }
;

There's no way out of the recursion, so it will keep generating more and more statements ad infinitum. 🙂


There's no C code yet because I want to get the program to at least compile properly, then I can worry about getting stuff done.


However, I have a new problem now. bison and flex both compile their respective inputs without errors, but when I go to link them together:

cc lex.yy.c syndata.tab.c -o comp

I get 40 or so of errors that all look like this:

lex.yy.c:640: error: called object '260' is not a function
lex.yy.c:641: error: expected ';' before '{' token
lex.yy.c: In function 'yy_get_next_buffer':
lex.yy.c:922: error: called object '260' is not a function
lex.yy.c:923: error: expected ';' before 'yy_fatal_error'
lex.yy.c:926: error: called object '260' is not a function
lex.yy.c:927: error: expected ';' before '{' token

and so on. I'm completely baffled here, since for instance, line 640 in lex.yy.c is as follows:

if ( !(yy_init) )

nothing more. While yy_int is indeed not a function (instead a static int), that should not be a problem for an if statement.
 
how to write a code for checking syntax error for end of message and end of chunk

Bumping a nearly five year-old thread isn't going to get you many responses. Suggest you post this as a new question, and add more detail.

Markbnj
Programming mod
 
Last edited by a moderator:
Back
Top