views:

72

answers:

2

I made a program that is supposed to recognize a simple grammar. When I input what I think is supposed to be a valid statement, I get an error. Specifically, if I type

int a;

int b;

it doesn't work. After I type int a; the program echoes ; for some reason. Then when I type int b; I get syntax error.

The lex file:

%{
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#include "y.tab.h"

%}

else ELSE
if IF
int INT|int
return RETURN
void VOID
while WHILE
id [a-zA-Z]*
num [0-9]*
lte <=
gte >=
equal ==
notequal !=

%%

{else}  {   return ELSE; }
{if}    {   return IF; }
{int}   { return INT; }
{return} {  return RETURN; }
{void} {    return VOID; }
{while} {   return WHILE; }
{id} {   return ID; }
{num} {  return NUM; }
{lte} {  return LTE; }
{gte} {  return GTE; }
{equal} {   return EQUAL; }
{notequal} {    return NOTEQUAL; }
%%

The yacc file:

/* C-Minus BNF Grammar */

%token ELSE
%token IF
%token INT
%token RETURN
%token VOID
%token WHILE

%token ID
%token NUM

%token LTE
%token GTE
%token EQUAL
%token NOTEQUAL
%%

program : declaration_list ;

declaration_list : declaration_list declaration | declaration ;

declaration : var_declaration | fun_declaration ;

var_declaration : type_specifier ID ';'
                | type_specifier ID '[' NUM ']' ';' ;

type_specifier : INT | VOID ;

fun_declaration : type_specifier ID '(' params ')' compound_stmt ;

params : param_list | VOID ;

param_list : param_list ',' param
           | param ;

param : type_specifier ID | type_specifier ID '[' ']' ;

compound_stmt : '{' local_declarations statement_list '}' ;

local_declarations : local_declarations var_declaration
                   | /* empty */ ;

statement_list : statement_list statement
               | /* empty */ ;

statement : expression_stmt
          | compound_stmt
          | selection_stmt
          | iteration_stmt
          | return_stmt ;

expression_stmt : expression ';'
                | ';' ;

selection_stmt : IF '(' expression ')' statement
               | IF '(' expression ')' statement ELSE statement ;

iteration_stmt : WHILE '(' expression ')' statement ;

return_stmt : RETURN ';' | RETURN expression ';' ;

expression : var '=' expression | simple_expression ;

var : ID | ID '[' expression ']' ;

simple_expression : additive_expression relop additive_expression
                  | additive_expression ;

relop : LTE | '<' | '>' | GTE | EQUAL | NOTEQUAL ;

additive_expression : additive_expression addop term | term ;

addop : '+' | '-' ;

term : term mulop factor | factor ;

mulop : '*' | '/' ;

factor : '(' expression ')' | var | call | NUM ;

call : ID '(' args ')' ;

args : arg_list | /* empty */ ;

arg_list : arg_list ',' expression | expression ;
+1  A: 

Ok...you need to add a semi-colon as a token as well in your language spec...as a fyi, do a google search on this ...there are a few lex/yacc files for C programming language as well...and there are plenty of tutorials on this...flex/bison are not exactly forgiving on program spec errors...you really need to understand the elements of how it works...Look for Jack Crenshaw's famous tutorial on how to build a compiler.

tommieb75
If I added a semi-colon, what would I return? Do I need to change all instances of ';' in the yacc file, or can I leave them like that?
Phenom
+1  A: 

Lex:

id [a-zA-Z]*
num [0-9]*

both cases can meet empty strings, use '+' instead

Dewfy
Doesn't fix the problem, but that's true.
Phenom
@Phenom - append following line to lex: "newline \n" By default space treatment cannot recognize \r \n combination
Dewfy