Я нашел грамматику 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: "'".
Чего не хватает?
Это похоже на мою работу, сделанную несколько лет назад. Он был предназначен для модифицированной версии лимона, которую я создал, но больше не поддерживаю, что создало парсер в Lua.
Чтобы использовать его с готовым анализатором лимона, вам придется заменить все экземпляры цитируемых литералов именованными токенами. Смотрите раздел Терминалы и Нетерминалы в лимонная документация. (На этой странице есть ссылка.)
В отличие от yacc / bison, lemon не требует от вас объявлять имена токенов, потому что такие имена должны начинаться с заглавной буквы. Как и yacc / bison, lemon создаст заголовочный файл с определениями тех имен, которые вы можете #include
в вашей реализации сканера. (Использовать flex для создания сканера для анализаторов лимонов легко, но вы должны внимательно прочитать документацию по лимонам; анализаторы лимонов вызываются сканером, а не вызывать сканер.)
Есть несколько возможностей лимонов, которых нет в yacc / bison, которые усложнили бы преобразование этой грамматики для yacc / bison:
|
нотация в лимоне недокументирована, и отличается от использования |
в яц / бизоне; он может использоваться только для создания списка альтернативных терминальных токенов. В yacc / bison вы можете создать нетерминал этой формы, но нетерминалы не участвуют в правилах приоритета; у лимона список альтернатив — «Мультитерминал», который не мешает правилам приоритета. Так что в yacc / bison вам придется дублировать производство, используя альтернативы токенов для каждого альтернативного терминала. Другим подходом yacc / bison будет использование одного лексического токена для всех альтернатив в группе, различая их по их семантическому значению, которое может быть enum
, Это будет хорошо работать для операторских терминалов, но не будет работать для альтернатив, таких как ','|';'
потому что они не соответствуют всей грамматике.
Там нет эквивалента YACC / бизонов для %fallback
и при этом нет простого обходного пути.
Других решений пока нет …