gpt4 book ai didi

java - ANTLR 的基本帮助

转载 作者:行者123 更新时间:2023-12-02 08:18:07 25 4
gpt4 key购买 nike

我正在尝试创建一个解析器,它将英语句子翻译成 Canvas 上绘制的形状。例如:“创建一个红色框”应该在 Canvas 上创建一个红色框。

我从您的 wiki 教程中想出了这个语法文件。不知道是否正确,如果有人可以检查一下就好了 =)

grammar Shrdlu;

tokens {

//operational tokens
MOVE = 'move';
TRANSFORM = 'transform';
CREATE = 'create';
MAKE = 'make';
ADD = 'add';
REMOVE = 'remove';
DELETE = 'delete';

//shape tokens
BOX = 'box';
RECTANGLE = 'rectangel';
CIRCLE = 'circle';
TRIANGLE = 'triangle';
SHAPE = 'shape';
SQUARE = 'square';

//color tokens
RED = 'red';
BLUE = 'blue';
GREEN = 'green';
BLACK = 'black';
PURPLE = 'purple';
YELLOW = 'yellow';
ORANGE = 'orange';
PINK = 'pink';

//size tokens
BIG = 'big';
LARGE = 'large';
TALL = 'tall';
SMALL = 'small';
TINY = 'tiny';
SHORT = 'short';

//relation size
BIGGEST = 'biggest';
LARGEST = 'largest';
TALLEST = 'tallest';
SMALLEST = 'smallest';
SHORTEST = 'shortest';

//argument size
BIGGER = 'bigger';
SMALLER = 'smaller';
SHORTER = 'shorter';
TALLER = 'taller';
LARGER = 'larger';

//alignment tokens
LEFT = 'left';
RIGHT = 'right';
OVER = 'over';
UNDER = 'under';
ABOVE = 'above';
BELOW = 'below';
TOP = 'top';
BOTTOM = 'bottom';

//prefix tokens
A = 'a';
AN = 'an';
ALL = 'all';
ANY = 'any';
EACH = 'each';
THE = 'the';

}

/*------------------------------------------------------------------
* PARSER RULES
*------------------------------------------------------------------*/

command
: sentence EOF
|
;

sentence
: WS? action WS object (WS argument)? WS?
;

action

: MOVE
| TRANSFORM
| CREATE
| MAKE
| ADD
| REMOVE
| DELETE
;

object
: prefix WS (property WS)? shape (WS relation WS object)?
;

a rgument
: color
| sizearg
| alignment
;

prefix
: A
| AN
| ALL
| ANY
| EACH
| THE
;

shape
: BOX
| RECTANGLE
| CIRCLE
| TRIANGLE
| SHAPE
| SQUARE
;

property
: size (WS property)?
| color (WS property)?
;


size
: BIG
| LARGE
| TALL
| SMALL
| TINY
| SHORT
;

sizearg
: BIGGER
| LARGER
| SMALLER
| TALLER
| SHORTER
;

relsize
: BIGGEST
| SMALLEST
| TALLEST
| LARGEST
;


relation
: alignment
| relsize
;

color
: RED
| BLUE
| GREEN
| BLACK
| PURPLE
| YELLOW
| ORANGE
| PINK
;

alignment
: LEFT
| RIGHT
| OVER
| UNDER
| ABOVE
| BELOW
| TOP
| BOTTOM
;

/*------------------------------------------------------------------
* LEXER RULES
*------------------------------------------------------------------*/

NEWLINE
: '\r'? '\n'
;

WS
: (' '|'\t'|'\n'|'\r')+ {skip();}
;

然后我使用这段代码生成词法分析器和解析器。我下一步要做什么。例如,我如何使用解析器将“创建”转换为对象的创建。有人能指出我正确的方向吗?

最佳答案

由于 WS 规则中的 {skip();},您的词法分析器永远不会创建任何 WS 标记。因此,其中包含 WS 的解析器规则(如 sentence 规则)永远不会匹配。所以从这个意义上说,你的语法是错误的。

Richard wrote:

into the creation of an object.

编辑

您可以为解析器提供自定义属性,例如要在其上绘制的 Graphics 对象(请参阅 @parser::members { ... } 部分下面的语法)。并在 {} 之间的语法规则中嵌入自定义代码。作为演示,我只添加了一些 System.out.println,但当然,您应该将它们替换为您的实际绘画。我还从解析器规则中删除了 WS 标记(另请注意,Create 不等于 create!)。

修改后的语法:

grammar Shrdlu;

tokens {

//operational tokens
MOVE = 'move';
TRANSFORM = 'transform';
CREATE = 'create';
MAKE = 'make';
ADD = 'add';
REMOVE = 'remove';
DELETE = 'delete';

//shape tokens
BOX = 'box';
RECTANGLE = 'rectangel';
CIRCLE = 'circle';
TRIANGLE = 'triangle';
SHAPE = 'shape';
SQUARE = 'square';

//color tokens
RED = 'red';
BLUE = 'blue';
GREEN = 'green';
BLACK = 'black';
PURPLE = 'purple';
YELLOW = 'yellow';
ORANGE = 'orange';
PINK = 'pink';

//size tokens
BIG = 'big';
LARGE = 'large';
TALL = 'tall';
SMALL = 'small';
TINY = 'tiny';
SHORT = 'short';

//relation size
BIGGEST = 'biggest';
LARGEST = 'largest';
TALLEST = 'tallest';
SMALLEST = 'smallest';
SHORTEST = 'shortest';

//argument size
BIGGER = 'bigger';
SMALLER = 'smaller';
SHORTER = 'shorter';
TALLER = 'taller';
LARGER = 'larger';

//alignment tokens
LEFT = 'left';
RIGHT = 'right';
OVER = 'over';
UNDER = 'under';
ABOVE = 'above';
BELOW = 'below';
TOP = 'top';
BOTTOM = 'bottom';

//prefix tokens
A = 'a';
AN = 'an';
ALL = 'all';
ANY = 'any';
EACH = 'each';
THE = 'the';

}

@parser::members {

private java.awt.Graphics graphics;

public ShrdluParser(TokenStream tokens, java.awt.Graphics g) {
super(tokens);
graphics = g;
}
}

/*------------------------------------------------------------------
* PARSER RULES
*------------------------------------------------------------------*/

command
: sentence EOF
|
;

sentence
: action object argument?
;

action

: MOVE
| TRANSFORM
| CREATE {System.out.println("I should create a ...");}
| MAKE
| ADD
| REMOVE
| DELETE
;

object
: prefix property? shape (relation object)?
;

argument
: color
| sizearg
| alignment
;

prefix
: A
| AN
| ALL
| ANY
| EACH
| THE
;

shape
: BOX {System.out.println("box ...");}
| RECTANGLE
| CIRCLE
| TRIANGLE
| SHAPE
| SQUARE
;

property
: size property?
| color property?
;


size
: BIG
| LARGE
| TALL
| SMALL
| TINY
| SHORT
;

sizearg
: BIGGER
| LARGER
| SMALLER
| TALLER
| SHORTER
;

relsize
: BIGGEST
| SMALLEST
| TALLEST
| LARGEST
;


relation
: alignment
| relsize
;

color
: RED {System.out.println("red ...");}
| BLUE
| GREEN
| BLACK
| PURPLE
| YELLOW
| ORANGE
| PINK
;

alignment
: LEFT
| RIGHT
| OVER
| UNDER
| ABOVE
| BELOW
| TOP
| BOTTOM
;

NEWLINE
: '\r'? '\n'
;

WS
: (' '|'\t'|'\n'|'\r')+ {skip();}
;

可以使用以下类进行测试:

import org.antlr.runtime.*;

public class Main {
public static void main(String[] args) throws Exception {
ANTLRStringStream in = new ANTLRStringStream("create a red box");
ShrdluLexer lexer = new ShrdluLexer(in);
CommonTokenStream tokens = new CommonTokenStream(lexer);
java.awt.Graphics g = null;
ShrdluParser parser = new ShrdluParser(tokens, g);
parser.command();
}
}

根据语法创建词法分析器和解析器:

java -cp antlr-3.3.jar org.antlr.Tool Shrdlu.g

然后编译所有.java源文件:

javac -cp antlr-3.3.jar *.java

并运行主类:

# *nixjava -cp .:antlr-3.3.jar Main# Windowsjava -cp .;antlr-3.3.jar Main

产生以下输出:

I should create a ...red ...box ...

最后一件事,您可以删除 NEWLINE 规则:您的 WS 规则捕获此类字符(并忽略它们)。

关于java - ANTLR 的基本帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5968516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com