antlr-java-AST

所属分类:自动编程
开发工具:Java
文件大小:21KB
下载次数:0
上传日期:2020-03-03 15:21:57
上 传 者sh-1993
说明:  使用抽象语法树在antlr+java中实现定制编程语言
(Implementation of a custom programming language in antlr + java using abstract syntax trees)

文件列表:
changes.txt (1961, 2020-03-03)
src (0, 2020-03-03)
src\ASTVisitor.java (25759, 2020-03-03)
src\Data.java (752, 2020-03-03)
src\Main.java (911, 2020-03-03)
src\pascal.g4 (5643, 2020-03-03)
src\pascalVisitor.java (10135, 2020-03-03)
test.txt (1048, 2020-03-03)
test1.pas (91, 2020-03-03)
test10.pas (376, 2020-03-03)
test11.pas (345, 2020-03-03)
test12.pas (298, 2020-03-03)
test13.pas (305, 2020-03-03)
test14.pas (593, 2020-03-03)
test15.pas (699, 2020-03-03)
test16.pas (611, 2020-03-03)
test17.pas (658, 2020-03-03)
test18.pas (647, 2020-03-03)
test19.pas (657, 2020-03-03)
test2.pas (139, 2020-03-03)
test2.txt (275, 2020-03-03)
test20.pas (569, 2020-03-03)
test3.pas (189, 2020-03-03)
test3.txt (284, 2020-03-03)
test4.pas (250, 2020-03-03)
test4.txt (652, 2020-03-03)
test5.pas (251, 2020-03-03)
test5.txt (659, 2020-03-03)
test6.pas (175, 2020-03-03)
test6.txt (579, 2020-03-03)
test7.pas (181, 2020-03-03)
test8.pas (477, 2020-03-03)
test9.pas (141, 2020-03-03)

# antlr-java-AST Implementation of a custom programming language in antlr + java using abstract syntax trees ## About: In this project we use a java-based "visitor pattern" (visitor for short) to traverse the abstract syntax tree generated by our ANTLR grammar. ## Running: 1. Generate the proper java files from ANTLR grammar. Run the following command with the ```-visitor``` flag, to generate a visitor in addition to the rest of the files: ``` antlr4 pascal.g4 -visitor ``` 2. Compile generated java files: ``` javac pascal*.java ``` ## Notes ### What is a visitor? A visitor is simply the action implementer for our parse tree. It has a method for each branch, containing what actions to perform for that branch. Our base visitor, generated by the command ```-visitor``` above, is an *interface*; it is generated with empty methods for handling each branch. It's up to us to implement what actions they do in our ASTVisitor file. ### What is ```ctx```? The ctx argument is an instance of a specific class context for the node that we are entering/exiting. Our visitor visits each branch of the parse tree. Upon reaching a branch, the method for handling that branch takes in the branch as a *context*, named ```ctx``` for short. This ```ctx``` is how we access the branch's elements and do actions on it. Let's take the branch ```varDeclaration``` for example. As our visitor goes through the parse tree, it will come across the branch ```varDeclaration``` where we declare our variables. This will trigger the visitor's ```visitVarDeclaration``` method. The input parameter to this method is ```pascalParser.VarDeclarationContext ctx```, which is our current *context*. What is meant by context? It means all of the parse elements of the current branch. It defines what we can access. In the case of the ```varDeclaration``` branch, this could be ```vNameList()```, ```BOOLEANTYPE()```, or even ```COLON()```. By accessing the elements of our current *context*, we can perform actions on them. For example, I could do ```ctx.vNameList().getText()``` to get the text of the variable names. ### Scoping: - The project description was unclear on what exactly needed to carry over between scopes. We implemented a mix of all options to demonstrate functionality, although they might not match pascal exactly. Our implementation is as follows: - For/while loops: A new scope is created for these loops. Variables declared within loops are not transferred back to the parent scope. Parent scope variables are able to be affected by operations within the loop. - User-defined functions: A new scope is created for within functions. In functions, parent scope variables can NOT be affected by operations within the function. We understood functions to simply be about calculating the return value. - User-defined procedures: A new scope is created for within procedures. In procedures, parent scope variables CAN be affected by operations within the procedure, but only if that variable was passed in as a parameter. We chose not to allow all global variables to be affected, since that implementation would basically be the same thing as just putting the code in the main program block, which seemed pointless. # Misc. Assumptions: - As far as we could tell, case statements in pascal only have one line of execution for each case. Multi-line execution was implemented in if statements. # Bonus Implemented: - Implement parameter passing in procedures/functions. Procedures should be able to declare parameters that can be passed when they are called. These variables should be correctly scoped as well. ## To-Do: - [x] Implement visitor - [x] Program name - [x] Variable declarations - [x] Main program block - [x] Arithmetic expressions (with variables) - [x] Boolean expressions (with variables) - [x] If-then-else statements - [x] Case statements - [x] Special math expressions (sqrt, sin, cos, ln, exp) - [x] Readln - [x] Writeln - [x] Variable assignment - [x] Static scoping - [x] Global variables vs. local variables - [x] Scope chaining and position tracking - [x] While loops - [x] For loops - [x] Break/continue in loops - [x] User defined functions - [x] User defined procedures - [x] Formal parameter passing-in for functions and procedures (Bonus)

近期下载者

相关文件


收藏者