简体   繁体   English

VBA的无上下文语法

[英]Context-free grammar for VBA

I'm trying to scan and parse VBA (visual basic for application) code for a school assignment. 我正在尝试扫描和解析学校作业的VBA(应用程序的Visual Basic)代码。

I'm using Python PLY lex and yacc modules. 我正在使用Python PLY lex和yacc模块。 Right now I'm just trying to get variable declaration and assignments to work as a proof of concept. 现在,我只是想让变量声明和赋值用作概念证明。 My preliminary grammar understands a variable declaration OR an assignment. 我的初步语法理解变量声明或赋值。 As soon as I put a newline (\\n) character in there and add another statement it doesn't understand anything. 我在其中放置换行符(\\ n)并添加另一条语句后,它什么都不懂。 ie: In the code in the gist, if you remove "a = 3" from the string at line 92, it will work fine, and insert an Identifier in the identifier list. 即:在要点的代码中,如果从第92行的字符串中删除“ a = 3”,它将正常工作,并在标识符列表中插入一个标识符。

I handle newline characters in the scanner, so I think there's something wrong with my grammar definition, but can't figure it out. 我在扫描仪中处理换行符,因此我认为我的语法定义有问题,但无法弄清楚。

The grammar is basically this: 语法基本上是这样的:

statement : declaration
          | assignment

declaration : DIM IDENTIFIER AS TYPE

assignment : IDENTIFIER ASSIGN BOOLEAN
           | IDENTIFIER ASSIGN DOUBLE
           | IDENTIFIER ASSIGN INT

Note that IDENTIFIER, ASSIGN, BOOLEAN, DOUBLE, INT DIM, AS and TYPE are all tokens defined in the lex module. 请注意,IDENTIFIER,ASSIGN,BOOLEAN,DOUBLE,INT DIM,AS和TYPE都是在lex模块中定义的标记。

I've created a gist with the code which is at: https://gist.github.com/clsk/22c386695dd1ddb7ca75 用以下代码创建了要点https : //gist.github.com/clsk/22c386695dd1ddb7ca75

@rici wrote: @rici写道:

That's a grammar for a single statement. 这是单个语句的语法。 Why do you expect it to work with multiple statements? 为什么期望它与多个语句一起使用? Nowhere is there a production which indicate that multiple statements are legal input. 没有地方可以表明多个陈述是合法的输入。

The OP wrote: OP写道:

Indeed that was the issue. 确实,这就是问题所在。 Thanks for the pointer 谢谢你的指导

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

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