I am pretty new to lex and yacc. I was trying out a grammar to recognize the input text: ESCREVER 1;
or ESCREVER 1+2;
but everytime I try it shows me syntax error , I think the problem is parsing the number or the word ESCREVER
.
Here is my flex/lex code
%{
#include "gram.h"
int yyerror(const char *s);
%}
/* letras [A-Za-z]+ */
/* id ({letras})({letras}|{digito})* */
%%
"ESCREVER" {return ESCREVER; }
"TERMINAR" {return TERMINAR; }
[0-9]+ { yylval.num =atoi(yytext);
return NUM; }
[A-Za-z0-9]* { yylval.str=strdup(yytext);
return TEXTO;}
"/" |
"-" |
"+" |
"*" |
"=" |
. {return yytext[0];}
[ \n\t] { }
%%
int yywrap(){ return 1; }
YACC code:
%{
#include <stdio.h>
int yylex(void);
int yyerror(const char *s);
%}
%union{
char *str; /* para strings*/
int num; /* para inteiros */
}
%token TERMINAR ESCREVER
%token SUBTRACAO
%token MULTIPLICACAO
%token DIVISAO
%token SOMA
%token<num> NUM /*para inteiros*/
%token<str> TEXTO /*MUDAR PARA VAR*/
%type<num> elemento
%type<num> expr
%type<num> lista
%start s
%%
s:linha s
|TERMINAR ';' {return 0;}
;
linha: ESCREVER lista';' {printf("%d",$2);}
| VARS
;
lista: lista ',' elemento
| elemento
;
elemento:NUM
|expr
;
VARS :
| NUM
| TEXTO
| expr
| TEXTO '=' VARS ';' /* para delcaracoes */
;
expr : NUM SOMA expr {$$=$1+$3;}
| NUM SUBTRACAO expr {$$=$1-$3;}
| NUM MULTIPLICACAO expr {$$=$1*$3;}
| NUM DIVISAO expr {$$=$1/$3;}
| NUM '+' expr {$$=$1+$3;}
| NUM '=' expr {$$=$1=$3;}
| NUM '-' expr {$$=$1-$3;}
| NUM '*' expr {$$=$1*$3;}
| NUM '/' expr {$$=$1/$3;}
| NUM {$$=$1; }
;
%%
int yyerror(char const *s) {
fprintf(stderr,"Erro: %s\n",s);
return 0;
}
int main(int argc, char *argv[]) {
extern FILE *yyin;
if (argc > 1) {
if((yyin=fopen(argv[1],"r"))==NULL){
fprintf(stderr,"erro ao abrir o ficheiro \"%s\".\n",argv[1]);
return 1;
}
} else
printf("introduza comandos: \n");
yyparse();
return 0;
}
These two rows in your scanner specification both match space:
. {return yytext[0];}
[ \n\t] { }
When you type ESCREVER 1;
the space is interpreted by the first rule. Since Flex uses the rules in the order they are written, you can just move the blank-ignoring rule up, so it is checked first.