ANTLR4:在输入错误时没有可行的替代方案

时间:2015-04-11 20:08:49

标签: antlr pascal antlr4

我正在使用pascal语法的缩减版本来创建一个将pascal转换为javascript的编译器,但是我一直遇到这个错误

line 3:4 no viable alternative at input 'PROCEDURE'
line 3:38 extraneous input ':' expecting {'END', ';'}
line 5:4 no viable alternative at input 'VAR'

以下是我的语法的相关部分:

grammar pascal;

program
: programHeading ('INTERFACE')?
  block
  DOT
;

programHeading
: 'PROGRAM' identifier (LPAREN identifierList RPAREN)? SEMI
| 'UNIT' identifier SEMI
;

identifier
: IDENT
;

block
: ( labelDeclarationPart
  | constantDefinitionPart
  | typeDefinitionPart
  | variableDeclarationPart
  | procedureAndFunctionDeclarationPart
  | usesUnitsPart
  | 'IMPLEMENTATION'
  )*
   | compoundStatement
;
procedureAndFunctionDeclarationPart
: procedureOrFunctionDeclaration SEMI
;

procedureOrFunctionDeclaration
: procedureDeclaration
| functionDeclaration
;

procedureDeclaration
: 'PROCEDURE'  identifier (formalParameterList)? SEMI
  ( block | directive )
;

functionDeclaration
: 'FUNCTION' identifier (formalParameterList)? COLON resultType SEMI
  ( block | directive )
;

compoundStatement
: 'BEGIN'
statements
  'END'
;

statements
: statement ( SEMI statement )*
;

statement
: label COLON unlabelledStatement
| unlabelledStatement
;

我正在使用antlr-4.5-complete,并希望有人可以对此有所了解。

这是我试图编译的程序:

PROGRAM Lesson1_PROGRAM3;
BEGIN
PROCEDURE DrawLine(X : Integer; Y : Integer);

VAR
    Num1, Num2, Sum : Integer;

BEGIN
    Write('Input number 1:');
    Readln(Num1);
    Writeln('Input number 2:');
    Readln(Num2);
    Sum := Num1 + Num2;
    Writeln(Sum);
    Readln;

    IF Sel = '1' THEN
    BEGIN
        Total := N1 + N2;
        Write('Press any key TO continue...');
        Readkey;
        GOTO 1;
    END;

    FOR Counter := 1 TO 7 DO
        writeln('for loop');
    Readln;
END;
END.

0 个答案:

没有答案