- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在用简单的 C 为编译器编写解析器,我试图理解为什么我有
yacc:22 个 shift/reduce 冲突,15 个 reduce/reduce 冲突。
文件
letter [a-zA-Z]
Hexdigits [A-F|0-9]
OctalDigit [0-7]
digit [0-9]
digitMZ [1-9]
newline (\n)
integerNum "0"|{digitMZ}{digit}*
HexNumber ("0X"|"0x"){Hexdigits}+
OctalNumber ["0"]{OctalDigit}+
BinaryNumber ["0"|"1"]+"b"
identifier {letter}({letter}|{digit}|"_")*
char "'"."'"
wordString \"[^\"]*\"
Comment "/%"[^"%/"]+"%/"
StrDec "["{integerNum}"]"
%{
%}
%option yylineno
%%
"string"" "*{StrDec} { return STRING_DECLERATION;}
boolean { return BOOLEAN; }
else { return ELSE; }
if { return IF; }
integer { return INTEGER; }
procedure { return PROCEDURE; }
char { return CHAR; }
string { return STRING; }
var { return VAR; }
intptr { return INTPTR; }
charptr { return CHARPTR; }
return { return RETURN; }
void { return VOID; }
while { return WHILE; }
"isprint()" { return IS_PRINT; }
true|false { return BOOLCONSTANT; }
":" { return COLON; }
"+" { return PLUS; }
"&" { return ADDRESS; }
"^" { return DEREFERENCE; }
"-" { return MINUS; }
"*" { return MULTIPLICATION; }
"&&" { return AND; }
"/" { return DIVISION; }
"<=" { return LESSEQUAL; }
"<" { return LESS; }
">" { return GREATER; }
">=" { return GREATEREQUAL; }
"==" { return EQUAL; }
"!=" { return NOTEQUAL; }
"=" { return ASSIGN; }
";" { return SEMICOLON; }
"," { return COMMA; }
"(" { return LEFTPAREN; }
")" { return RIGHTPAREN; }
"[" { return LEFTBRACKET; }
"]" { return RIGHTBRACKET; }
"{" { return LEFTBRACE; }
"}" { return RIGHTBRACE; }
"||" { return OR; }
"!" { return NOT; }
" "+ { }
"|" {return ABS_LENGTH;}
" "+ {}
{newline} { }
{Comment} {}
{wordString} {return WORDSTRING; }
{integerNum} {return INTEGER_CONST; }
{char} {return CHAR_IDENTIFIER; }
{identifier} {return IDENTIFIER; }
{HexNumber} {return HEX_NUMBER; }
{OctalNumber} {return OCTAL_NUMBER; }
{BinaryNumber} {return BINARY_NUMBER; }
. {return ERROR; }
%%
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define YYDEBUG 1
#define YYSTYPE struct node*
int yyerror(char*);
int yylex();
typedef struct node
{
char * rtoken;
struct node * left;
struct node * right;
char * ltoken;
}node;
node * mknode(char * ltoken, node * left, node * right,char * rtoken);
void printtree(node * tree);
char * getLT(node *);
extern char *yytext;
%}
%token STRING_DECLERATION
%token STRING
%token WORDSTRING
%token BOOLEAN
%token BREAK
%token ELSE
%token IF
%token IMPLEMENTS
%token INTEGER
%token PROCEDURE
%token CHAR
%token VAR
%token INTPTR
%token CHARPTR
%token RETURN
%token VOID
%token WHILE
%token IS_PRINT
%token BOOLCONSTANT
%token PLUS
%token COLON
%token ADDRESS
%token DEREFERENCE
%token MINUS
%token MULTIPLICATION
%token AND
%token DIVISION
%token LESS
%token LESSEQUAL
%token GREATER
%token GREATEREQUAL
%token EQUAL
%token NOTEQUAL
%token ASSIGN
%token SEMICOLON
%token COMMA
%token LEFTPAREN
%token RIGHTPAREN
%token LEFTBRACKET
%token RIGHTBRACKET
%token LEFTBRACE
%token RIGHTBRACE
%token OR
%token NOT
%token ABS_LENGTH
%token CHAR_IDENTIFIER
%token HEX_NUMBER
%token OCTAL_NUMBER
%token BINARY_NUMBER
%token ERROR
%token INTEGER_CONST
%token IDENTIFIER
%%
start: s{printtree($1);}
;
s: expr {$$=$1;}
|expr s {node *yu = mknode("",$1,NULL,"\n");$$= mknode("",yu,$2,"");}
;
expr: function {$$=$1;}
|assignment SEMICOLON {$$=$1;}
|decleration SEMICOLON {$$=$1;}
|condition {$$=$1;}
|BRACES {$$=$1;}
;
function: PROCEDURE iden LEFTPAREN argListOpt RIGHTPAREN RETURN identifiertype compondStmt
{node *z =mknode("RETURN:",$7,NULL,"\n");node *o =mknode(getLT($2),$4,z,""); $$ = mknode("(PROCEDURE",o,$8,"\n)\n");}
|PROCEDURE iden LEFTPAREN RIGHTPAREN RETURN identifiertype compondStmt
{node *z2 = mknode("",NULL,NULL,"");node *z1 =mknode("RETURN:",$6,NULL,"\n");
node *o =mknode(getLT($2),z2,z1,""); $$ = mknode("(PROCEDURE",o,$7,"\n)\n");}
;
argListOpt: multidentifier COLON identifiertype SEMICOLON argListOpt
{node *fg =mknode("",$1,$3,""); $$ = mknode("recive:",fg,$5,"");}
|multidentifier COLON identifiertype{$$ = mknode("recive:",$1,$3,"");}
;
compondStmt: BRACES{$$=$1;}
|SEMICOLON{$$ = mknode("",NULL,NULL,"");}
;
functionCall: iden LEFTPAREN sendArgs RIGHTPAREN{node *Sgl = mknode("(",$3,NULL,")");$$= mknode("",$1,Sgl,"");}
|iden LEFTPAREN RIGHTPAREN {node *Sgg = mknode("(",NULL,NULL,")");$$= mknode("",$1,Sgg,"");}
;
sendArgs: return_val {$$=$1;}
|sendArgs COMMA return_val {node *SA = mknode("",$1,NULL,",");$$= mknode("",SA,$3,"");}
;
BRACES: LEFTBRACE All RIGHTBRACE {$$ = mknode("\n{BRACES\n",$2,NULL,"\n}");}
|LEFTBRACE RIGHTBRACE{$$ = mknode("\n{BRACES\n",NULL,NULL,"\n}");}
|LEFTBRACE BRACES RIGHTBRACE{$$ = mknode("\n{BRACES\n",$2,NULL,"\n}");}
;
All: RETURN return_val SEMICOLON{$$ = mknode("RETURN",$2,NULL,"\n");}
|All RETURN return_val SEMICOLON{node * K = mknode("RETURN",$2,NULL,"");$$ = mknode("",$1,K,"\n");}
|All function{$$ = mknode("",$1,$2,"\n");}
|function {$$=$1;}
|assignment {$$ = mknode("(",$1,NULL,")\n");}
|assignment SEMICOLON {$$ = mknode("(",$1,NULL,")\n");}
|decleration SEMICOLON{$$=$1;}
|decleration{$$=$1;}
|All decleration SEMICOLON {$$ = mknode("",$1,$2,"\n");}
|All assignment SEMICOLON {$$ = mknode("",$1,$2,"\n");}
|condition{$$=$1;}
|All condition{$$ = mknode("",$1,$2,"\n");}
|All BRACES {$$ = mknode("",$1,$2,"\n");}
|BRACES All {$$ = mknode("",$1,$2,"\n");}
;
return_val: int_num{$$=$1;}
|iden {$$=$1;}
|wstring{$$ = $1;}
|exp{$$=$1;}
|char_id{$$ = mknode(yytext,NULL,NULL,"");}
|bool{$$=$1;}
|wordlength{$$=$1;}
|absolute{$$=$1;}
|identifierabs{$$=$1;}
;
decleration: VAR multidentifier COLON identifiertype {$$ = mknode("",$2,$4,"\n");}
;
multidentifier: iden {$$=$1;}
| multidentifier COMMA iden{node *er = mknode("",$1,NULL,",");$$= mknode("",er,$3,"");}
;
bool: BOOLCONSTANT{$$ = mknode(yytext,NULL,NULL,"");}
;
assignment : iden ASSIGN functionCall {node *pop = mknode("=",$1,NULL,"");$$ = mknode("\t(",pop,$3,")");}
| iden ASSIGN exp {node *rop = mknode("=",$1,NULL,"");$$ = mknode("\t(",rop,$3,")");}
| iden ASSIGN bool{node *nop = mknode("=",$1,NULL,"");$$ = mknode("\t(",nop,$3,")");}
| iden ASSIGN pointeroperator iden
{node *gh= mknode("",$3,$4,"");node *e1 = mknode("=",$1,NULL,"");$$ = mknode("\t(",e1,gh,")");}
| iden ASSIGN iden {node *e2 = mknode("=",$1,NULL,"");$$ = mknode("\t(",e2,$3,")");}
| iden ASSIGN wstring {node *e3 = mknode("=",$1,NULL,"");$$ = mknode("\t(",e3,$3,")");}
| iden ASSIGN char_id{node *e4 = mknode("=",$1,NULL,"");$$ = mknode("\t(",e4,$3,")");}
| iden ASSIGN "null"{node *e5 = mknode("=",$1,NULL,"");$$ = mknode("\t(",e5,NULL,"NULL)");}
;
char_id: CHAR_IDENTIFIER {$$ = mknode(yytext,NULL,NULL,"");}
;
exp: int_num {$$=$1;}
|exp methoperator exp {node *y = mknode(getLT($2),$1,NULL,"");$$ = mknode("(",y,$3,")");}
|LEFTPAREN exp methoperator exp RIGHTPAREN {node *y = mknode(getLT($3),$2,NULL,"");$$ = mknode("(",y,$4,")");}/**/
|iden {$$=$1;}
|LEFTPAREN exp RIGHTPAREN {$$ = mknode("(",$2,NULL,")");}/**/
;
condition: whileCond{$$=$1;}
|ifCond{$$=$1;}
;
ifCond: IF LEFTPAREN innercond RIGHTPAREN BRACES{$$ = mknode("(COND \n",$3,$5,"\n)\n");}
|IF LEFTPAREN innercond RIGHTPAREN BRACES ELSE BRACES{node *yk= mknode("ELSE",$7,NULL,"");
node *j= mknode("",$5,yk,"");$$ = mknode("(COND \n",$3,j,"\n)\n");}
;
whileCond: WHILE LEFTPAREN innercond RIGHTPAREN BRACES {$$ = mknode("(LOOP \n",$3,$5,"\n)\n");}
;
wstring: WORDSTRING{$$ = mknode(yytext,NULL,NULL,"");}
;
condexp: exp signoperator exp {$$ = mknode(getLT($2),$1,$3,"");}
|wstring IsEqual wstring {$$ = mknode(getLT($2),$1,$3,"");}
|iden signoperator exp{$$ = mknode(getLT($2),$1,$3,"");}
|bool{$$=$1;}
|iden IsEqual wstring {$$ = mknode(getLT($2),$1,$3,"");}
|wstring IsEqual iden{$$ = mknode(getLT($2),$1,$3,"");}
|char_id IsEqual char_id {$$ = mknode(getLT($2),$1,$3,"");}
|iden IsEqual char_id {$$ = mknode(getLT($2),$1,$3,"");}
|char_id IsEqual iden{$$ = mknode(getLT($2),$1,$3,"");}
|exp signoperator wordlength {$$ = mknode(getLT($2),$1,$3,"");}
|wordlength signoperator exp {$$ = mknode(getLT($2),$1,$3,"");}
|exp signoperator absolute{$$ = mknode(getLT($2),$1,$3,"");}
|absolute signoperator exp{$$ = mknode(getLT($2),$1,$3,"");}
|wordlength signoperator absolute{$$ = mknode(getLT($2),$1,$3,"");}
|absolute signoperator wordlength {$$ = mknode(getLT($2),$1,$3,"");}
|identifierabs signoperator exp {$$ = mknode(getLT($2),$1,$3,"");}
|identifierabs signoperator wordlength{$$ = mknode(getLT($2),$1,$3,"");}
|identifierabs signoperator absolute{$$ = mknode(getLT($2),$1,$3,"");}
|identifierabs signoperator identifierabs{$$ = mknode(getLT($2),$1,$3,"");}
;
wordlength: ABS_LENGTH wstring ABS_LENGTH {$$ = mknode("|",$2,NULL,"|");}
;
absolute : ABS_LENGTH int_num ABS_LENGTH{$$ = mknode("|",$2,NULL,"|");}
;
identifierabs: ABS_LENGTH iden ABS_LENGTH {$$ = mknode("|",$2,NULL,"|");}
;
innercond: condexp {$$=mknode("\t(",$1,NULL,")");}
|LEFTPAREN condexp RIGHTPAREN{$$ = mknode("(",$2,NULL,")");}
|LEFTPAREN condexp RIGHTPAREN condoperator innercond{node *t = mknode("",$4,$5,"");$$ = mknode("(",$2,t,")");}
|condexp condoperator innercond
{node *lolo = mknode("(",$1,NULL,"");node *Sr = mknode(getLT($2),lolo,NULL,")");$$= mknode(" ",Sr,$3,"");}
|LEFTPAREN condexp condoperator innercond RIGHTPAREN
{node *lili = mknode("(",$2,NULL,"");node *Sr = mknode(getLT($3),lili,NULL,",");$$= mknode(" ",Sr,$4,")");}
;
methoperator: PLUS {$$ = mknode(yytext,NULL,NULL,"");}
|MINUS {$$ = mknode(yytext,NULL,NULL,"");}
|MULTIPLICATION {$$ = mknode(yytext,NULL,NULL,"");}
|DIVISION {$$ = mknode(yytext,NULL,NULL,"");}
;
pointeroperator:ADDRESS {$$ = mknode(yytext,NULL,NULL,"");}
|DEREFERENCE {$$ = mknode(yytext,NULL,NULL,"");}
;
identifiertype: STRING_DECLERATION {$$ = mknode(yytext,NULL,NULL,"");}
|BOOLEAN {$$ = mknode(yytext,NULL,NULL,"");}
|INTEGER {$$ = mknode(yytext,NULL,NULL,"");}
|CHAR {$$ = mknode(yytext,NULL,NULL,"");}
|INTPTR {$$ = mknode(yytext,NULL,NULL,"");}
|CHARPTR {$$ = mknode(yytext,NULL,NULL,"");}
|int_num{$$=$1;}
;
iden : IDENTIFIER {$$ = mknode(yytext,NULL,NULL,"");}
;
signoperator: LESS {$$ = mknode(yytext,NULL,NULL,"");}
|LESSEQUAL {$$ = mknode(yytext,NULL,NULL,"");}
|GREATER {$$ = mknode(yytext,NULL,NULL,"");}
|GREATEREQUAL {$$ = mknode(yytext,NULL,NULL,"");}
|IsEqual {$$=$1;}
;
IsEqual: EQUAL {$$ = mknode(yytext,NULL,NULL,"");}
|NOTEQUAL {$$ = mknode(yytext,NULL,NULL,"");}
;
condoperator: AND {$$ = mknode(yytext,NULL,NULL,"");}
|OR {$$ = mknode(yytext,NULL,NULL,"");}
;
int_num: INTEGER_CONST {$$ = mknode(yytext,NULL,NULL,"");}
|BINARY_NUMBER {$$ = mknode(yytext,NULL,NULL,"");}
|OCTAL_NUMBER {$$ = mknode(yytext,NULL,NULL,"");}
|HEX_NUMBER {$$ = mknode(yytext,NULL,NULL,"");}
;
%%
#include "lex.yy.c"
int main()
{
return yyparse();
}
node* mknode(char * ltoken, node * left, node * right,char * rtoken)
{
node * newnode = (node*)malloc(sizeof(node));
char * newstr = (char*)malloc(sizeof(ltoken)+1);
strcpy(newstr, ltoken);
char * newstrr = (char*)malloc(sizeof(rtoken)+1);
strcpy(newstrr, rtoken);
newnode->left = left;
newnode->right = right;
newnode->ltoken = newstr;
newnode->rtoken = newstrr;
return newnode;
}
void printtree(node * tree)
{
printf("%s ", tree->ltoken);
if (tree->left)
printtree(tree->left);
if (tree->right)
printtree(tree->right);
printf("%s ", tree->rtoken);
}
int yyerror(char* s)
{
extern int yylineno;
printf("yacc error: %s at line %d\n",s,yylineno); return 0;
}
char * getLT(node * s)
{
return s->ltoken;
}
最佳答案
冲突不是错误,它们基本上意味着解析器生成器无法决定在某些阶段应用哪个规则。移位/减少冲突是指您可以完成规则(减少)或使用下一个 token (移位)并从该状态继续。一个典型的 shift/reduce 问题是 if-else 语句:
if condition
if another condition
# Do something...
else
# Do something else
IF <condition> <statement> IF <condition> <statement> ELSE <statement>
The parser is here ^
sequence:
%empty { printf ("empty sequence\n"); }
| maybeword
| sequence word { printf ("added word %s\n", $2); }
;
maybeword:
%empty { printf ("empty maybeword\n"); }
| word { printf ("single word %s\n", $1); }
;
关于compiler-errors - 简单 C 语法中的移位/减少冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41349749/
reqwest v0.9 将 serde v1.0 作为依赖项,因此实现 converting serde_json errors into reqwest error . 在我的代码中,我使用 se
我有这个代码: let file = FileStorage { // ... }; file.write("Test", bytes.as_ref()) .map_err(|e| Mu
我只是尝试用angular-cli创建一个新项目,然后运行服务器,但是它停止并显示一条有趣的消息:Error: No errors。 我以这种方式更新了(希望有帮助):npm uninstall -g
我从我的 javascript 发送交易 Metamask 打开传输对话框 我确定 i get an error message in metamask (inpage.js:1 MetaMask -
这个问题在这里已经有了答案: How do you define custom `Error` types in Rust? (3 个答案) How to get a reference to a
我想知道两者之间有什么大的区别 if let error = error{} vs if error != nil?或者只是人们的不同之处,比如他们如何用代码表达自己? 例如,如果我使用这段代码: u
当我尝试发送超过 50KB 的图像时,我在 Blazor 服务器应用程序上收到以下错误消息 Error: Connection disconnected with error 'Error: Serv
我有一个error-page指令,它将所有异常重定向到错误显示页面 我的web.xml: [...] java.lang.Exception /vi
我有这样的对象: address: { "phone" : 888, "value" : 12 } 在 WHERE 中我需要通过 address.value 查找对象,但是在 SQL 中有函数
每次我尝试编译我的代码时,我都会遇到大量错误。这不是我的代码的问题,因为它在另一台计算机上工作得很好。我尝试重新安装和修复,但这没有帮助。这是整个错误消息: 1>------ Build starte
在我的代码的类部分,如果我写一个错误,则在不应该的情况下,将有几行报告为错误。我将'| error'放在可以从错误中恢复的良好/安全位置,但是我认为它没有使用它。也许它试图在某个地方恢复中间表情? 有
我遇到了 csv 输入文件整体读取故障的问题,我可以通过在 read_csv 函数中添加 "error_bad_lines=False" 来删除这些问题来解决这个问题。 但是我需要报告这些造成问题的文
在 Spring 中,验证后我们在 controller 中得到一个 BindingResult 对象。 很简单,如果我收到验证错误,我想重新显示我的表单,并在每个受影响的字段上方显示错误消息。 因此
我不知道出了什么问题,因为我用 Java 编程了大约一年,从来没有遇到过这个错误。在一分钟前在 Eclipse 中编译和运行工作,现在我得到这个错误: #A fatal error has been
SELECT to_char(messages. TIME, 'YYYY/MM/DD') AS FullDate, to_char(messages. TIME, 'MM/DD
我收到这些错误: AnonymousPath\Anonymized.vb : error BC30037: Character is not valid. AnonymousPath\Anonymiz
我刚刚安装了 gridengine 并在执行 qstat 时出现错误: error: commlib error: got select error (Connection refused) erro
嗨,我正在学习 PHP,我从 CRUD 系统开始,我在 Windows 上安装了 WAMP 服务器,当我运行它时,我收到以下错误消息。 SCREAM: Error suppression ignore
我刚刚开始一个新项目,我正在学习核心数据教程,可以找到:https://www.youtube.com/watch?v=zZJpsszfTHM 我似乎无法弄清楚为什么会抛出此错误。我有一个名为“Exp
当我使用 Jenkins 运行新构建时,出现以下错误: "FilePathY\XXX.cpp : fatal error C1853: 'FilePathZ\XXX.pch' precompiled
我是一名优秀的程序员,十分优秀!