When I'm trying to check the expression "boolean x;" I'm getting "syntax error" and I can't understand why. When I'm checking the expression "x = 3;" or "2 = 1;", the abstract syntax tree is generated and no errors are presented. (I'm not allowed to use anything beside Lex and Yacc in this project and I'm using Ubuntu)
Lex file:
%%
[\n\t ]+;
boolean {return BOOL;}
TRUE {return TRUE;}
FALSE {return FALSE;}
[0-9]+ {return NUM;}
[a-zA-Z][0-9a-zA-Z]* {return ID;}
. {return yytext[0];}
%%
Yacc file:
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node{
struct node *left;
struct node *right;
char *token;
} node;
node *mknode(node *left, node *right, char *token);
void printtree(node *tree);
#define YYSTYPE struct node *
%}
%start code
%token ID,NUM,TRUE,FALSE,BOOL
%right '='
%%
code:lines{printtree($1); printf("\n");}
lines:calcExp';'|assignExp';'|boolExp ';'{$$ = $1;}
boolExp: boolST id{$$=$2;}
calcExp: number '+' number {$$ = mknode($1,$3,"+");}
assignExp: id '=' number{$$ = mknode($1,$3,"=");}
boolSt : BOOL;
id : ID {$$ = mknode(0,0,yytext);}
number : NUM{$$ = mknode(0,0,yytext);}
%%
#include "lex.yy.c"
int main (void) {return yyparse();}
node *mknode(node *left, node *right, char *token){
node *newnode = (node *)malloc(sizeof(node));
char *newstr = (char *)malloc(strlen(token)+1);
strcpy(newstr, token);
newnode->left = left;
newnode->right = right;
newnode->token = newstr;
return newnode;
}
void printtree(node *tree){
if (tree->left || tree->right)
printf("(");
printf(" %s ", tree->token);
if(tree->left)
printtree(tree->left);
if(tree->right)
printtree(tree->right);
if(tree->left || tree->right)
printf(")");
}
void yyerror (char *s) {
fprintf (stderr, "%s\n",s);}
boolSt: BOOL
does not define the symbolboolST
.) It's not clear to me why you feel you need a non-terminal here at all; you could have writtenboolExp: BOOL id
. Or, if you had used%token BOOL "boolean"
, you could use the even more readableboolExp: "boolean" id
. – rici[\n\t ]
will match newline, tab or space. So will[ \n\t]
. But[\n\t]
only matches newline or tab. Your question has the correct character class, but fails for the reason noted by sepp2k. Nowhere do you indicate that you removed the space in the character class. – rici