- Jun 17, 2005
- 927
- 1
- 81
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;
}
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
|
;
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;
}