- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的语法师需要具有用户定义的Type ID组合。以下代码的问题在于它会生成以下内容:
[java] Warning : *** Shift/Reduce conflict found in state #67
[java] between VariableDecls ::= (*)
[java] and Type ::= (*) ID
[java] under symbol ID
[java] Resolved in favor of shifting.
[java] Warning : *** Shift/Reduce conflict found in state #65
[java] between VariableDecls ::= (*)
[java] and Type ::= (*) ID
[java] under symbol ID
[java] Resolved in favor of shifting.
[java] Error : *** More conflicts encountered than expected -- parser generation aborted
[java] ------- CUP v0.11b 20160615 (GIT 4ac7450) Parser Generation Summary -------
[java] 1 error and 4 warnings
[java] 51 terminals, 34 non-terminals, and 93 productions declared,
[java] producing 190 unique parse states.
[java] 2 terminals declared but not used.
[java] 0 non-terminals declared but not used.
[java] 0 productions never reduced.
[java] 2 conflicts detected (0 expected).
[java] No code produced.
import java_cup.runtime.*;
import java.io.IOException;
import java.io.File;
import java.io.FileInputStream;
parser code {:
TScanner scanner;
Parser(TScanner scanner) { this.scanner = scanner; }
public void syntax_error(Symbol cur_token) {
System.out.println("WE ARE HERE");
done_parsing();
}
public void unrecovered_syntax_error(Symbol cur_token) {
System.out.println(cur_token.sym);
System.out.println("[reject]");
}
:}
scan with {: return scanner.next_token(); :};
/* Terminals (tokens returned by the scanner). */
terminal BOOLN, DBL, _INT, STRING, NUL;
terminal _IF, ELS, FR, WHLE;
terminal INTCONST, DBLCONST, STRINGCONST, BOOLCONST;
terminal ADDOP, SUBOP, MULOP, DIV, MOD;
terminal LFTPRN, RTPRN, LFTBRACKET, RTBRC, LFTBRACE, RTBRACE;
terminal LESS, LESSEQ, GRT, GRTEQ, EQL, NEQ;
terminal AND, OR, NOT;
terminal ASSIGN, SEMICOL, COMMA, DOT;
terminal BRK, CLS, EXTNDS, IMPL, INTRFC, NEWAR;
terminal PRNTLN, READLN, RTRN, _VOID, NW;
terminal ID;
/* Non terminals */
non terminal Program, Decls, Decl;
non terminal VariableDecl, FunctionDecl, ClassDecl, InterfaceDecl;
non terminal Variable, Type, Formals, Variables, Extends, Implements, Implement;
non terminal Field, Fields, Prototype, StmtBlock, VariableDecls, Stmts, Stmt;
non terminal OptionExpr, WhileStmt, ForStmt, BreakStmt;
non terminal ReturnStmt, PrintStmt, Expr, Exprs, Lvalue, Call, Actuals, Constant;
non terminal IfStmt;
/* Precedences */
precedence right ASSIGN;
precedence left OR;
precedence left AND;
precedence left EQL, NEQ;
precedence left LESS, LESSEQ, GRT, GRTEQ;
precedence left ADDOP, SUBOP;
precedence left MULOP, DIV, MOD;
precedence left NOT;
precedence left LFTBRACKET, DOT;
precedence left ELS;
/* Toy grammar */
start with Program;
Program ::=
Decls
{: System.out.print("[reduce 1]"); System.out.print("[accept]"); done_parsing(); :};
Decls ::=
Decl
{: System.out.print("[reduce 2]"); :}
| Decl Decls
{: System.out.print("[reduce 3]"); :} ;
Decl ::=
VariableDecl
{: System.out.print("[reduce 4]"); :}
| FunctionDecl
{: System.out.print("[reduce 5]"); :}
| ClassDecl
{: System.out.print("[reduce 6]"); :}
| InterfaceDecl
{: System.out.print("[reduce 7]"); :} ;
VariableDecl ::=
Variable SEMICOL
{: System.out.print("[reduce 8]"); :} ;
Variable ::=
Type ID
{: System.out.print("[reduce 9]"); :} ;
Type ::=
_INT
{: System.out.print("[reduce 10]"); :}
| DBL
{: System.out.print("[reduce 11]"); :}
| BOOLN
{: System.out.print("[reduce 12]"); :}
| STRING
{: System.out.print("[reduce 13]"); :}
| Type LFTBRACKET RTBRC
{: System.out.print("[reduce 14]"); :}
| ID {: System.out.print("[reduce 15]"); :};
FunctionDecl ::=
Type ID LFTPRN Formals RTPRN StmtBlock
{: System.out.print("[reduce 16]"); :}
| _VOID ID LFTPRN Formals RTPRN StmtBlock
{: System.out.print("[reduce 17]"); :} ;
Formals ::=
// EMPTY
{: System.out.print("[reduce 18]"); :}
| Variables
{: System.out.print("[reduce 19]"); :} ;
Variables ::=
Variable
{: System.out.print("[reduce 20]"); :}
| Variable COMMA Variables
{: System.out.print("[reduce 21]"); :} ;
ClassDecl ::=
CLS ID Extends Implements LFTBRACE Fields RTBRACE
{: System.out.print("[reduce 22]"); :} ;
Extends ::=
// EMPTY
{: System.out.print("[reduce 23]"); :}
| EXTNDS ID
{: System.out.print("[reduce 24]"); :};
Implements ::=
// EMPTY
{: System.out.print("[reduce 25]"); :}
| Implement
{: System.out.print("[reduce 26]"); :};
Implement ::=
IMPL ID
{: System.out.print("[reduce 27]"); :}
| IMPL ID COMMA Implement
{: System.out.print("[reduce 28]"); :};
Fields ::=
// EMPTY
{: System.out.print("[reduce 29]"); :}
| Field Fields
{: System.out.print("[reduce 30]"); :};
Field ::=
VariableDecl
{: System.out.print("[reduce 31]"); :}
| FunctionDecl
{: System.out.print("[reduce 32]"); :};
InterfaceDecl ::=
INTRFC ID LFTBRACE Prototype RTBRACE
{: System.out.print("[reduce 33]"); :};
Prototype ::=
// EMPTY
{: System.out.print("[reduce 34]"); :}
| Type ID LFTPRN Formals RTPRN SEMICOL Prototype
{: System.out.print("[reduce 35]"); :}
| _VOID ID LFTPRN Formals RTPRN SEMICOL Prototype
{: System.out.print("[reduce 36]"); :};
StmtBlock ::=
LFTBRACE VariableDecls Stmts RTBRACE
{: System.out.print("[reduce 37]"); :};
VariableDecls ::=
//EMPTY
{:System.out.print("[reduce 38]"); :}
|
VariableDecl VariableDecls
{: System.out.print("[reduce 39]"); :};
Stmts ::=
// EMPTY
{: System.out.print("[reduce 40]"); :}
| Stmt Stmts
{: System.out.print("[reduce 41]"); :};
Stmt ::=
OptionExpr SEMICOL
{: System.out.print("[reduce 42]"); :}
| IfStmt
{: System.out.print("[reduce 43]"); :}
| WhileStmt
{: System.out.print("[reduce 44]"); :}
| ForStmt
{: System.out.print("[reduce 45]"); :}
| BreakStmt
{: System.out.print("[reduce 46]"); :}
| ReturnStmt
{: System.out.print("[reduce 47]"); :}
| PrintStmt
{: System.out.print("[reduce 48]"); :}
| StmtBlock
{: System.out.print("[reduce 49]"); :};
IfStmt ::=
_IF LFTPRN Expr RTPRN Stmt
{: System.out.print("[reduce 50]"); :}
| _IF LFTPRN Expr RTPRN Stmt ELS Stmt
{: System.out.print("[reduce 51]"); :};
WhileStmt ::=
WHLE LFTPRN Expr RTPRN Stmt
{: System.out.print("[reduce 52]"); :};
ForStmt ::=
FR LFTPRN OptionExpr SEMICOL Expr SEMICOL OptionExpr RTPRN Stmt
{: System.out.print("[reduce 53]"); :};
BreakStmt ::=
BRK SEMICOL
{: System.out.print("[reduce 54]"); :};
ReturnStmt ::=
RTRN OptionExpr SEMICOL
{: System.out.print("[reduce 55]"); :};
PrintStmt ::=
PRNTLN LFTPRN Exprs RTPRN SEMICOL
{: System.out.print("[reduce 56]"); :};
Expr ::=
Lvalue ASSIGN Expr
{: System.out.print("[reduce 57]"); :}
| Constant
{: System.out.print("[reduce 58]"); :}
| Lvalue
{: System.out.print("[reduce 59]"); :}
| Call
{: System.out.print("[reduce 60]"); :}
| LFTPRN Expr RTPRN
{: System.out.print("[reduce 61]"); :}
| Expr ADDOP Expr
{: System.out.print("[reduce 62]"); :}
| Expr SUBOP Expr
{: System.out.print("[reduce 63]"); :}
| Expr MULOP Expr
{: System.out.print("[reduce 64]"); :}
| Expr DIV Expr
{: System.out.print("[reduce 65]"); :}
| Expr MOD Expr
{: System.out.print("[reduce 66]"); :}
| Expr LESS Expr
{: System.out.print("[reduce 68]"); :}
| Expr LESSEQ Expr
{: System.out.print("[reduce 69]"); :}
| Expr GRT Expr
{: System.out.print("[reduce 70]"); :}
| Expr GRTEQ Expr
{: System.out.print("[reduce 71]"); :}
| Expr EQL Expr
{: System.out.print("[reduce 72]"); :}
| Expr NEQ Expr
{: System.out.print("[reduce 73]"); :}
| Expr AND Expr
{: System.out.print("[reduce 74]"); :}
| Expr OR Expr
{: System.out.print("[reduce 75]"); :}
| NOT Expr
{: System.out.print("[reduce 76]"); :}
| READLN LFTPRN RTPRN
{: System.out.print("[reduce 77]"); :}
| NEWAR LFTPRN INTCONST COMMA Type RTPRN
{: System.out.print("[reduce 78]"); :};
Lvalue ::=
ID
{: System.out.print("[reduce 79]"); :}
| Lvalue LFTBRACKET Expr RTBRC
{: System.out.print("[reduce 80]"); :}
| Lvalue DOT ID
{: System.out.print("[reduce 81]"); :};
Call ::=
ID LFTPRN Actuals RTPRN
{: System.out.print("[reduce 82]"); :}
| ID DOT ID LFTPRN Actuals RTPRN
{: System.out.print("[reduce 83]"); :};
Actuals ::=
// EMPTY
{: System.out.print("[reduce 84]"); :}
| Exprs
{: System.out.print("[reduce 85]"); :};
Exprs ::=
Expr
{: System.out.print("[reduce 86]"); :}
| Expr COMMA Exprs
{: System.out.print("[reduce 87]"); :};
Constant ::=
INTCONST
{: System.out.print("[reduce 88]"); :}
| DBLCONST
{: System.out.print("[reduce 89]"); :}
| STRINGCONST
{: System.out.print("[reduce 90]"); :}
| BOOLCONST
{: System.out.print("[reduce 91]"); :};
OptionExpr ::=
//EMPTY
{: System.out.print("[reduce 92]"); :}
| Expr
{: System.out.print("[reduce 93]"); :};
最佳答案
我认为这是流行的“ Decaf”语言的某种变体,经常在CS入门课程中使用。
对我来说还不是很清楚,为什么CUP只报告两个冲突,因为按照您的语法,您的语法中有四个冲突。也许您粘贴的版本不是生成包含在问题中的错误消息的版本。
错误消息中报告的冲突是由于对变量声明列表和组成语句块的语句列表都使用了右递归的结果。
传统知识将告诉您,应尽可能避免进行右递归,因为它使用了无限数量的解析器堆栈。相比之下,左递归使用恒定数量的解析器堆栈。这是一个很好的经验法则,但是在大多数情况下,左递归和右递归之间的选择将由语法决定。因此,例如,如果您在不使用优先级声明的情况下为算术表达式编写语法,则将对左关联运算符(几乎全部使用左递归)和对右递归运算符(例如赋值运算符)使用右递归C,C ++和Java)。
项目列表通常可以以任何一种方式编写,因为它们通常会折叠成一个向量,而不是停留在二叉树上,因此正常情况下将保留递归:
x_list ::= x_list x_element |
// EMPTY
;
x_list: x_element
。如果元素后面必须有标记或由标记分隔,则还必须进行修改,因此您经常会看到以下内容:
// In the language this comes from, statements are *always* followed by
// a semicolon. Most languages don't work that way, though.
statement_list ::= statement_list statement T_SEMICOLON |
// EMPTY
;
// Parameter lists (and argument lists) are either empty or have the items
// *separated* by commas. Because the comma is only present if there are at
// least two items, we need to special-case the empty list:
parameter_list ::= T_OPAREN T_CPAREN |
T_OPAREN parameters T_CPAREN
;
parameters ::= parameter |
parameters T_COMMA parameter
;
id_list ::= id_list ID | id_list ::= ID id_list |
// EMPTY // EMPTY
; ;
a b c
,但是他们以不同的方式接受它们:
•3 •3
/ \ / \
•2 c a •2
/ \ / \
•1 b b •1
/ \ / \
ε a c ε
StatementBody ::= OBRACE VariableDeclarations Statements CBRACE
VariableDeclarations ::= VariableDeclaration VariableDelarations | // EMPTY
Statements ::= Statement Statements | // EMPTY
Statements
和
Declarations
都需要有效地以空生产结尾。换句话说,在解析器可以移动
Statements
中的第一个令牌之前,它需要减少一个空的
VariableDeclarations
非终结符。这意味着它需要确切地知道哪个令牌将是
Statements
中的第一个令牌。
Statement
和
VariableDeclaration
都可以以
ID
开头。因此,如果解析器刚刚到达
VariableDeclaration
的末尾,并且超前标记为
ID
,则它无法判断是切换到解析
Statements
还是继续解析
VariableDeclarations
。
Statements
非终结符。避免使解析器猜测要在何处插入空非终端的唯一方法是将两个空非终端都放在
StatementBody
的末尾。换句话说,
VariableDeclarations
必须是左递归的,以便空的
VariableDeclarations
在开头,而
Statements
必须是右递归的,以便空的
Statements
在结尾:
StatementBody ::= OBRACE VariableDeclarations Statements CBRACE
VariableDeclarations ::= VariableDeclarations VariableDelaration | // EMPTY
Statements ::= Statement Statements | // EMPTY
Statement
还是以
VariableDeclaration
开头的
ID
。
ID
。在那里,它将遇到以下不确定性:
b [ ] a; // Declaration
b [ 3 ] = a; // Assignment
b
转换为
Lvalue
。
[ ]
作为单个标记返回来强制词法扫描器执行此工作。可以肯定地解决了这个问题-有了这一更改,单个开括号始终表示解析器正在查看表达式,而
[ ]
对始终表示声明。但这在扫描仪中很尴尬。特别是,扫描仪将需要能够处理类似
[ /* A comment */
/* Another comment */ ]
[ ]
令牌。 (我们希望没有人会编写这样的代码,但这是合法的。)
a . b ( 3 ) ;
a . b = 3 ;
ID DOT ID OPAREN ...
,而分配将匹配
Lvalue DOT ID
。换句话说,当
DOT
为超前时,解析器需要确定是否将
a
缩小为
Lvalue
。可以通过使两个右侧更加相似来避免这种情况。
关于java - 当用户定义变量或函数的类型时,Java CUP(解析器)会产生移位/减少冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53576949/
我一直在使用 AJAX 从我正在创建的网络服务中解析 JSON 数组时遇到问题。我的前端是一个简单的 ajax 和 jquery 组合,用于显示从我正在创建的网络服务返回的结果。 尽管知道我的数据库查
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我在尝试运行 Android 应用程序时遇到问题并收到以下错误 java.lang.NoClassDefFoundError: com.parse.Parse 当我尝试运行该应用时。 最佳答案 在这
有什么办法可以防止etree在解析HTML内容时解析HTML实体吗? html = etree.HTML('&') html.find('.//body').text 这给了我 '&' 但我想
我有一个有点疯狂的例子,但对于那些 JavaScript 函数作用域专家来说,它看起来是一个很好的练习: (function (global) { // our module number one
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 8 年前。 Improve th
我需要编写一个脚本来获取链接并解析链接页面的 HTML 以提取标题和其他一些数据,例如可能是简短的描述,就像您链接到 Facebook 上的内容一样。 当用户向站点添加链接时将调用它,因此在客户端启动
在 VS Code 中本地开发时,包解析为 C:/Users//AppData/Local/Microsoft/TypeScript/3.5/node_modules/@types//index而不是
我在将 json 从 php 解析为 javascript 时遇到问题 这是我的示例代码: //function MethodAjax = function (wsFile, param) {
我在将 json 从 php 解析为 javascript 时遇到问题 这是我的示例代码: //function MethodAjax = function (wsFile, param) {
我被赋予了将一种语言“翻译”成另一种语言的工作。对于使用正则表达式的简单逐行方法来说,源代码过于灵活(复杂)。我在哪里可以了解更多关于词法分析和解析器的信息? 最佳答案 如果你想对这个主题产生“情绪化
您好,我在解析此文本时遇到问题 { { { {[system1];1;1;0.612509325}; {[system2];1;
我正在为 adobe after effects 在 extendscript 中编写一些代码,最终变成了 javascript。 我有一个数组,我想只搜索单词“assemble”并返回整个 jc3_
我有这段代码: $(document).ready(function() { // }); 问题:FB_RequireFeatures block 外部的代码先于其内部的代码执行。因此 who
背景: netcore项目中有些服务是在通过中间件来通信的,比如orleans组件。它里面服务和客户端会指定网关和端口,我们只需要开放客户端给外界,服务端关闭端口。相当于去掉host,这样省掉了些
1.首先贴上我试验成功的代码 复制代码 代码如下: protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
什么是 XML? XML 指可扩展标记语言(eXtensible Markup Language),标准通用标记语言的子集,是一种用于标记电子文件使其具有结构性的标记语言。 你可以通过本站学习 X
【PHP代码】 复制代码 代码如下: $stmt = mssql_init('P__Global_Test', $conn) or die("initialize sto
在SQL查询分析器执行以下代码就可以了。 复制代码代码如下: declare @t varchar(255),@c varchar(255) declare table_cursor curs
前言 最近练习了一些前端算法题,现在做个总结,以下题目都是个人写法,并不是标准答案,如有错误欢迎指出,有对某道题有新的想法的友友也可以在评论区发表想法,互相学习🤭 题目 题目一: 二维数组中的
我是一名优秀的程序员,十分优秀!