简体   繁体   English

没有可行的输入ANTLR4

[英]No viable input ANTLR4

currently having some parsing issues with my grammar and I just can't figure out what is going wrong.. 目前在语法方面存在一些解析问题,但我无法弄清楚出了什么问题。

Here's my grammar: 这是我的语法:

grammar Demo;

@header {
    import java.util.List;
    import java.util.ArrayList;
}

program:
    functionList                                                                #programFunction
    ;

functionList:
    function*
    ;

function:
    'haupt()' '{' stmntList '}'                                                     #hauptFunction
    | type='zahl' ID '(' paramList ')' '{' stmntList '}'                            #zahlFunction
    | type='Zeichenkette' ID '(' paramList ')' '{' stmntList '}'                    #zeichenketteFunction
    | type='nix' ID '(' paramList ')' '{' stmntList '}'                             #nixFunction
    ;

paramList:
    param (',' paramList)?
    ;

param:
    'zahl' ID                                       
    | 'Zeichenkette' ID     
    |                                   
    ;

variableList:
    ID (',' variableList)?
    ;


stmntList:
    stmnt (stmntList)?                                      
    ;

stmnt:
    'zahl' varName=ID ';'                                                               #zahlStmnt
    | 'Zeichenkette' varName=ID ';'                                                     #zeichenketteStmnt
    |  varName=ID '=' varValue=expr ';'                                                 #varAssignment
    | 'Schreibe' '(' argument=expr ')' ';'                                  #schreibeImmediat 
    | 'Schreibe''(' argument=ID ')'     ';'                                     #schreibeText
    | 'zuZeichenkette' '(' ID ')'';'                                            #convertString
    | 'zuZahl''('ID')'';'                                                       #convertInteger
    | 'wenn' '(' boolExpr ')' '{' stmntList '}'  ('sonst' '{' stmntList '}')?   #wennsonstStmnt 
    | 'fuer' '(' ID '=' expr ',' boolExpr ',' stmnt ')' '{' stmntList '}'       #forLoop    
    | 'waehrend' '(' boolExpr ')' '{' stmntList '}'                             #whileLoop
    | 'tu' '{' stmntList '}' 'waehrend' '(' boolExpr ')'    ';'                     #doWhile
    | 'return' expr             ';'                                             #returnVar
    | fctName=ID '(' (variableList)? ')'';'                                     #functionCall               
    ;

boolExpr:
    boolParts ('&&' boolExpr)?                                                  #logicAnd
    | boolParts ('||' boolExpr)?                                                #logicOr
    ;

boolParts:
    expr '==' expr                                                              #isEqual
    | expr '!=' expr                                                            #isUnequal
    | expr '>' expr                                                             #biggerThan
    | expr '<' expr                                                             #smallerThan
    | expr '>=' expr                                                            #biggerEqual
    | expr '<=' expr                                                            #smallerEqual
    ;

expr:
    links=expr '+' rechts=product                   #addi
    | links = expr '-' rechts=product               #diff
    |product                            #prod
    ;

product:
    links=product '*' rechts=factor                 #mult
    | links=product '/' rechts=factor               #teil
    | factor                            #fact
    ;

factor:
    '(' expr')'                         #bracket
    | var=ID                                #var
    | zahl=NUMBER                           #numb
    ;


ID  :       [a-zA-Z]+;
NUMBER  :   '0'|[1-9][0-9]*;
WS:         [\r\n\t ]+ -> skip ;

And this is the code I am trying to parse: 这是我要解析的代码:

haupt() {
    zahl zz; 
    zz = 2;
    zahl cc;
    cc = 3;

    zz = zz+cc;
    Schreibe(cc+cc+cc);     
}   

the problems arise already in the first row, telling me that it expects a '{' instead of ' '. 问题已经出现在第一行,告诉我它期望使用'{'而不是''。 This is something I cannot understand since I skipped all WS in my grammar. 这是我无法理解的,因为我跳过了语法中的所有WS。 Next errors are the wrong recognition of the 2nd row: the variable declaration of "zahl zz;" 接下来的错误是第二行的错误识别:变量声明“ zahl zz;”。 is not understood as it should be: the first grammar rule of stmnt should work it, but it does not... 不应该被理解为:stmnt的第一个语法规则应该起作用,但是它不起作用...

Here are the errors antlrs TestRig gives me: 这是Antlrs TestRig给我的错误:

line 2:6 no viable alternative at input 'zahlzz'
line 4:6 no viable alternative at input 'zahlcc'
line 9:12 mismatched input '+' expecting ')'

Thanks for your help! 谢谢你的帮助! Tim 提姆

When I see a weird nonsensical bugaboo like this, it generally means that there is a token mismatch between what the lexer and parser think the token types are. 当我看到像这样的奇怪的,毫无意义的bugaboo时,通常意味着在词法分析器和解析器认为的令牌类型之间存在令牌不匹配。 Make sure the you regenerate all of your grammars using ANTLR and recompile everything. 确保您使用ANTLR重新生成所有语法并重新编译所有内容。 Hopefully that will clear it up. 希望这将清除它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM