我找到了Lemon here的Lua 5.1语法(页面末尾的清单1):
%fallback OPEN '(' .
chunk ::= block .
semi ::= ';' .
semi ::= .
block ::= scope statlist .
block ::= scope statlist laststat semi .
ublock ::= block 'until' exp .
scope ::= .
scope ::= scope statlist binding semi.
statlist ::= .
statlist ::= statlist stat semi .
stat ::= 'do' block 'end' .
stat ::= 'while' exp 'do' block 'end' .
stat ::= repetition 'do' block 'end' .
stat ::= 'repeat' ublock .
stat ::= 'if' conds 'end' .
stat ::= 'function' funcname funcbody .
stat ::= setlist '=' explist1 .
stat ::= functioncall .
repetition ::= 'for' NAME '=' explist23 .
repetition ::= 'for' namelist 'in' explist1 .
conds ::= condlist .
conds ::= condlist 'else' block .
condlist ::= cond .
condlist ::= condlist 'elseif' cond .
cond ::= exp 'then' block .
laststat ::= 'break' .
laststat ::= 'return' .
laststat ::= 'return' explist1 .
binding ::= 'local' namelist .
binding ::= 'local' namelist '=' explist1 .
binding ::= 'local' 'function' NAME funcbody .
funcname ::= dottedname .
funcname ::= dottedname ':' NAME .
dottedname ::= NAME .
dottedname ::= dottedname '.' NAME .
namelist ::= NAME .
namelist ::= namelist ',' NAME .
explist1 ::= exp .
explist1 ::= explist1 ',' exp .
explist23 ::= exp ',' exp .
explist23 ::= exp ',' exp ',' exp .
%left 'or' .
%left 'and' .
%left '<' '<=' '>' '>=' '==' '~=' .
%right '..' .
%left '+' '-' .
%left '*' '/' '%' .
%right 'not' '#' .
%right '^' .
exp ::= 'nil'|'true'|'false'|NUMBER|STRING|'...' .
exp ::= function .
exp ::= prefixexp .
exp ::= tableconstructor .
exp ::= 'not'|'#'|'-' exp . ['not']
exp ::= exp 'or' exp .
exp ::= exp 'and' exp .
exp ::= exp '<'|'<='|'>'|'>='|'=='|'~=' exp .
exp ::= exp '..' exp .
exp ::= exp '+'|'-' exp .
exp ::= exp '*'|'/'|'%' exp .
exp ::= exp '^' exp .
setlist ::= var .
setlist ::= setlist ',' var .
var ::= NAME .
var ::= prefixexp '[' exp ']' .
var ::= prefixexp '.' NAME .
prefixexp ::= var .
prefixexp ::= functioncall .
prefixexp ::= OPEN exp ')' .
functioncall ::= prefixexp args .
functioncall ::= prefixexp ':' NAME args .
args ::= '(' ')' .
args ::= '(' explist1 ')' .
args ::= tableconstructor .
args ::= STRING .
function ::= 'function' funcbody .
funcbody ::= params block 'end' .
params ::= '(' parlist ')' .
parlist ::= .
parlist ::= namelist .
parlist ::= '...' .
parlist ::= namelist ',' '...' .
tableconstructor ::= '{' '}' .
tableconstructor ::= '{' fieldlist '}' .
tableconstructor ::= '{' fieldlist ','|';' '}' .
fieldlist ::= field .
fieldlist ::= fieldlist ','|';' field .
field ::= exp .
field ::= NAME '=' exp .
field ::= '[' exp ']' '=' exp .
我尝试用lemon -c lua51.y
编译它,但是遇到了很多错误:
lua51.y:3: %fallback argument "'" should be a token
lua51.y:3: %fallback argument "(" should be a token
lua51.y:3: %fallback argument "'" should be a token
lua51.y:7: Illegal character on RHS of rule: "'".
lua51.y:12: Illegal character on RHS of rule: "'".
lua51.y:20: Illegal character on RHS of rule: "'".
lua51.y:21: Illegal character on RHS of rule: "'".
lua51.y:22: Illegal character on RHS of rule: "'".
lua51.y:23: Illegal character on RHS of rule: "'".
lua51.y:24: Illegal character on RHS of rule: "'".
lua51.y:25: Illegal character on RHS of rule: "'".
lua51.y:26: Illegal character on RHS of rule: "'".
lua51.y:29: Illegal character on RHS of rule: "'".
lua51.y:30: Illegal character on RHS of rule: "'".
lua51.y:33: Illegal character on RHS of rule: "'".
lua51.y:35: Illegal character on RHS of rule: "'".
lua51.y:36: Illegal character on RHS of rule: "'".
lua51.y:38: Illegal character on RHS of rule: "'".
lua51.y:39: Illegal character on RHS of rule: "'".
lua51.y:40: Illegal character on RHS of rule: "'".
lua51.y:42: Illegal character on RHS of rule: "'".
lua51.y:43: Illegal character on RHS of rule: "'".
lua51.y:44: Illegal character on RHS of rule: "'".
lua51.y:47: Illegal character on RHS of rule: "'".
lua51.y:50: Illegal character on RHS of rule: "'".
lua51.y:50: Token "'" should be either "%" or a nonterminal name.
lua51.y:50: Token "NAME" should be either "%" or a nonterminal name.
lua51.y:50: Token "." should be either "%" or a nonterminal name.
lua51.y:53: Illegal character on RHS of rule: "'".
lua51.y:56: Illegal character on RHS of rule: "'".
lua51.y:57: Illegal character on RHS of rule: "'".
lua51.y:58: Illegal character on RHS of rule: "'".
lua51.y:60: Can't assign a precedence to "'".
lua51.y:60: Can't assign a precedence to "or".
lua51.y:60: Can't assign a precedence to "'".
lua51.y:61: Can't assign a precedence to "'".
lua51.y:61: Can't assign a precedence to "and".
lua51.y:61: Can't assign a precedence to "'".
lua51.y:62: Can't assign a precedence to "'".
lua51.y:62: Can't assign a precedence to "<".
lua51.y:62: Can't assign a precedence to "'".
lua51.y:62: Can't assign a precedence to "'".
lua51.y:62: Can't assign a precedence to "<".
lua51.y:62: Can't assign a precedence to "=".
lua51.y:62: Can't assign a precedence to "'".
lua51.y:62: Can't assign a precedence to "'".
lua51.y:62: Can't assign a precedence to ">".
lua51.y:62: Can't assign a precedence to "'".
lua51.y:62: Can't assign a precedence to "'".
lua51.y:62: Can't assign a precedence to ">".
lua51.y:62: Can't assign a precedence to "=".
lua51.y:62: Can't assign a precedence to "'".
lua51.y:62: Can't assign a precedence to "'".
lua51.y:62: Can't assign a precedence to "=".
lua51.y:62: Can't assign a precedence to "=".
lua51.y:62: Can't assign a precedence to "'".
lua51.y:62: Can't assign a precedence to "'".
lua51.y:62: Can't assign a precedence to "~".
lua51.y:62: Can't assign a precedence to "=".
lua51.y:62: Can't assign a precedence to "'".
lua51.y:63: Can't assign a precedence to "'".
lua51.y:63: Token "." should be either "%" or a nonterminal name.
lua51.y:63: Token "'" should be either "%" or a nonterminal name.
lua51.y:63: Token "." should be either "%" or a nonterminal name.
lua51.y:64: Can't assign a precedence to "'".
lua51.y:64: Can't assign a precedence to "+".
lua51.y:64: Can't assign a precedence to "'".
lua51.y:64: Can't assign a precedence to "'".
lua51.y:64: Can't assign a precedence to "-".
lua51.y:64: Can't assign a precedence to "'".
lua51.y:65: Can't assign a precedence to "'".
lua51.y:65: Can't assign a precedence to "*".
lua51.y:65: Can't assign a precedence to "'".
lua51.y:65: Can't assign a precedence to "'".
lua51.y:65: Can't assign a precedence to "/".
lua51.y:65: Can't assign a precedence to "'".
lua51.y:65: Can't assign a precedence to "'".
lua51.y:65: Can't assign a precedence to "%".
lua51.y:65: Can't assign a precedence to "'".
lua51.y:66: Can't assign a precedence to "'".
lua51.y:66: Can't assign a precedence to "not".
lua51.y:66: Can't assign a precedence to "'".
lua51.y:66: Can't assign a precedence to "'".
lua51.y:66: Can't assign a precedence to "#".
lua51.y:66: Can't assign a precedence to "'".
lua51.y:67: Can't assign a precedence to "'".
lua51.y:67: Can't assign a precedence to "^".
lua51.y:67: Can't assign a precedence to "'".
lua51.y:69: Illegal character on RHS of rule: "'".
lua51.y:69: Token "." should be either "%" or a nonterminal name.
lua51.y:69: Token "." should be either "%" or a nonterminal name.
lua51.y:69: Token "'" should be either "%" or a nonterminal name.
lua51.y:69: Token "." should be either "%" or a nonterminal name.
lua51.y:73: Illegal character on RHS of rule: "'".
lua51.y:73: The precedence symbol must be a terminal.
lua51.y:73: Missing "]" on precedence mark.
lua51.y:73: Token "'" should be either "%" or a nonterminal name.
lua51.y:73: Token "]" should be either "%" or a nonterminal name.
lua51.y:74: Illegal character on RHS of rule: "'".
lua51.y:75: Illegal character on RHS of rule: "'".
lua51.y:76: Illegal character on RHS of rule: "'".
lua51.y:77: Illegal character on RHS of rule: "'".
lua51.y:77: Token "." should be either "%" or a nonterminal name.
lua51.y:77: Token "'" should be either "%" or a nonterminal name.
lua51.y:77: Expected to see a ":" following the LHS symbol "exp".
lua51.y:79: Illegal character on RHS of rule: "'".
lua51.y:79: Illegal declaration keyword: "'".
lua51.y:80: Illegal character on RHS of rule: "'".
lua51.y:83: Illegal character on RHS of rule: "'".
lua51.y:86: Illegal character on RHS of rule: "'".
lua51.y:87: Illegal character on RHS of rule: "'".
lua51.y:87: Token "'" should be either "%" or a nonterminal name.
lua51.y:87: Token "NAME" should be either "%" or a nonterminal name.
lua51.y:87: Token "." should be either "%" or a nonterminal name.
lua51.y:91: Illegal character on RHS of rule: "'".
lua51.y:94: Illegal character on RHS of rule: "'".
lua51.y:96: Illegal character on RHS of rule: "'".
lua51.y:97: Illegal character on RHS of rule: "'".
lua51.y:101: Illegal character on RHS of rule: "'".
lua51.y:103: Illegal character on RHS of rule: "'".
lua51.y:105: Illegal character on RHS of rule: "'".
lua51.y:109: Illegal character on RHS of rule: "'".
lua51.y:109: Token "." should be either "%" or a nonterminal name.
lua51.y:109: Token "." should be either "%" or a nonterminal name.
lua51.y:109: Token "'" should be either "%" or a nonterminal name.
lua51.y:109: Token "." should be either "%" or a nonterminal name.
lua51.y:110: Illegal character on RHS of rule: "'".
lua51.y:110: Token "." should be either "%" or a nonterminal name.
lua51.y:110: Token "." should be either "%" or a nonterminal name.
lua51.y:110: Token "'" should be either "%" or a nonterminal name.
lua51.y:110: Token "." should be either "%" or a nonterminal name.
lua51.y:112: Illegal character on RHS of rule: "'".
lua51.y:113: Illegal character on RHS of rule: "'".
lua51.y:114: Illegal character on RHS of rule: "'".
lua51.y:117: Illegal character on RHS of rule: "'".
lua51.y:120: Illegal character on RHS of rule: "'".
lua51.y:121: Illegal character on RHS of rule: "'".
缺少什么?
答案 0 :(得分:5)
为了将它与开箱即用的柠檬解析器一起使用,您必须使用命名的标记替换所有带引号的文字实例。请参阅lemon documentation中有关终端和非终端的部分。 (在您链接的页面中有关于此的注释。)
与yacc / bison不同,lemon不要求您声明令牌名称,因为这些名称必须以大写字母开头。与yacc / bison一样,lemon会生成一个头文件,其中包含这些名称的定义,您可以在扫描仪实现中#include
。 (使用flex很容易为柠檬解析器生成扫描仪,但是你应该仔细阅读柠檬文档;扫描仪调用柠檬解析器而不是调用扫描仪。)
yacc / bison中没有一些柠檬功能,这会使yacc / bison的语法转换变得复杂:
柠檬中的|
符号没有记录,与yacc / bison中|
的使用不同;它只能用于创建替代终端令牌列表。在yacc / bison中,您可以创建此表单的非终端,但非终端不参与优先规则;在柠檬中,替代品清单是&#34; Multiterminal&#34;它不会干扰优先规则。因此,在yacc / bison中,您必须使用每个替代终端的令牌替代品来复制产品。另一种yacc / bison方法是对一个组中的所有替代方法使用单个词法标记,通过它们的语义值区分它们,这可以是enum
。这对于操作员终端来说可以正常工作,但不适用于','|';'
之类的替代方案,因为这些方法在整个语法中都不一致。
%fallback
没有yacc / bison等价物,也没有简单的解决办法。