"boolean x;"という式をチェックしようとしていますが、私は "構文エラー"を取得していると私はなぜ理解できません。 "x = 3;"という式をチェックしているときに、または "2 = 1;"の場合、抽象構文ツリーが生成され、エラーは表示されません。YaccとLex "構文エラー"
%%
[\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のファイル:
%{
#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);}
スペル:それは "bool"か "boolean"ですか? –
ああ申し訳ありません。私は式 "boolean x;"をチェックしようとしています。 – SophieVi
この読みにくい混乱をフォーマットしてください。 – EJP