ANTLR语法后缀 [英] ANTLR grammar postfix

查看:127
本文介绍了ANTLR语法后缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遇到一个问题时,我正在为一种小语言编写一个简单的语法.当我尝试解释它时,它会抛出一个NullPointerException.哦,这是ANTLRWorks 3.

I was writing a simple grammar for a small language when I came across an issue. It's throwing a NullPointerException when I try to interpret it. Oh, and this is ANTLRWorks 3.

还-我指定了该语言应为Python,但它仍然在Java中起作用:(为什么?

Also - I specified that the language should be Python, but it still does stuff in Java :( Why?

以下是输入内容:

program Test1 = 
const t1 : int := 1;
const t2 : int := 2;
var x, y, z : int;
begin
x := 41;
y := 10;
z := 2 4 *;
end Test1.

这是我的代码:

grammar lang;

options {
    language=Python;
    output=AST;
    ASTLabelType=CommonTree;
}

tokens {DECL;} // an imaginary node

start : decl ;

decl : type ID ';' -> ^(DECL type ID)
     ;
type : INTTYPE  // automatic tree construction builds a node for this rule
     | FLOATTYPE
     ;
program
    : 'program' ID '='
    (const | variable)*
    'begin'
    statement*
    'end' ID '.'
    ;
const
    : 'const' ID ':' type ':=' expr ';'
    ;
variable
    : 'var' ID (',' ID)* ':' type ';'
    ;
statement
    : assignment
    ;
assignment
    : ID ':=' expr ';'
    ;



// expressions....fun!

term
    : ID
    | expr
    | INTTYPE
    ;
negation 
    : 'not'* term
    ;
unary
    : ('+' | '-')* negation
    ;
mult
    : unary (unary ('*' | '/' | 'mod'))*
    ;
add
    : mult (mult ('+' | '-'))*
    ;
relation
    : add (add ('==' | '!=' | '<' | '<=' | '>=' | '>'))*
    ;

expr
    : relation (relation ('and' | 'or'))* 
    ;

INTTYPE : 'int' ;
FLOATTYPE : 'float' ;
ID : ('a'..'z' | 'A'..'Z') ('a'..'z' | 'A'..'Z' | '0'..'9')* ;
INT : '0'..'9'+ ;
WS : (' '|'\n' | '\t') {$channel=HIDDEN;} ;

我在做什么错了?

推荐答案

正如Kay所述:无需在后缀表达式中考虑运算符优先级.后缀表达式只是一个或多个操作数和运算符的列表.这是使语法正常运行的方法:

As Kay already mentioned: there is no need to account for operator precedence in post-fix expressions. Post-fix expressions are just a list of one or more operands and operators. Here's how to get your grammar working:

grammar lang;

options {
  language=Python;
  output=AST;
}

tokens {
  PROGRAM;
  STATS;
  DECL;
  ASSIGN;
  EXPR;
}

program
  :  'program' id=ID '=' decl* 'begin' statement* 'end' ID '.'
     -> ^(PROGRAM $id ^(DECL decl*) ^(STATS statement*))
  ;

decl
  :  const 
  |  variable
  ;

type 
  :  INTTYPE 
  |  FLOATTYPE
  ;

const
  :  'const' ID ':' type ':=' expr ';' -> ^('const' type ID expr)
  ;

variable
  :  'var' ID (',' ID)* ':' type ';' -> ^('var' type ID+)
  ;

statement
  :  assignment
  ;

assignment
  :  ID ':=' expr ';' -> ^(ASSIGN ID expr)
  ;

expr
  :  exprAtom+ -> ^(EXPR exprAtom+)
  ;

exprAtom
  :  operand 
  |  operator
  ;

operand
  :  INT
  |  ID
  ;

operator
  :  'and' | 'or' | '==' | '!=' | '<' | '<=' | '>=' | '>' | '+' | '-' | '*' | '/' | 'mod' | 'not'
  ;

INTTYPE   : 'int' ;
FLOATTYPE : 'float' ;
ID        : ('a'..'z' | 'A'..'Z') ('a'..'z' | 'A'..'Z' | '0'..'9')* ;
INT       : '0'..'9'+ ;
WS        : (' '|'\n' | '\t') {$channel=HIDDEN;} ;

现在通过在命令行上执行以下操作,从中生成一个词法分析器和解析器(Python源文件!):

Now generate a lexer and parser (Python source files!) from it by executing the following on the command line:

java -cp antlr-3.1.3.jar org.antlr.Tool lang.g

如果您现在执行以下脚本

And if you now execute the following script

#!/usr/bin/env python
import antlr3
from antlr3 import *
from antlr3.tree import *
from langLexer import *
from langParser import *

def print_level_order(tree, indent):
  print '{0}{1}'.format('   '*indent, tree.text)
  for child in tree.getChildren():
    print_level_order(child, indent+1)

input = """
program Test1 = 
const t1 : int := 1;
const t2 : int := 2;
var x, y, z : int;
begin
x := 41;
y := 10;
z := 2 4 *;
end Test1.
"""
char_stream = antlr3.ANTLRStringStream(input)
lexer = langLexer(char_stream)
tokens = antlr3.CommonTokenStream(lexer)
parser = langParser(tokens)
tree = parser.program().tree 
print_level_order(tree, 0)

您将看到以下内容打印到控制台:

you'll see the following being printed to the console:

PROGRAM
   Test1
   DECL
      const
         int
         t1
         EXPR
            1
      const
         int
         t2
         EXPR
            2
      var
         int
         x
         y
         z
   STATS
      ASSIGN
         x
         EXPR
            41
      ASSIGN
         y
         EXPR
            10
      ASSIGN
         z
         EXPR
            2
            4
            *

这篇关于ANTLR语法后缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆