The basic framework of this project is forked by the sample code from "Lark Grammar to AST and Symbol Table". Here is its original description: A small example of parsing some parts of the Quack language (class and method declarations), transforming the Lark parse tree into an AST with our own custom classes, and then walking the AST to populate a symbol table with class and method declarations. Thanks to Pranav Mathur for the initial symbol table of built-in classes (in qklib/builtin_methods.json
).I have followed that structure in filling in additional classes and methods from the source file.
This is the second project of CS 461. Its requirement includes:
- Parse some basic sentences for Quack Language
- Not include control flow
no if statements, no while statements - Each Assignment includes the Type Declaration
Example:x: Int = 13 + y;
- Hold Static Checking
Example:x: Int = "Why would you " + "do this?" - 3;
we will translate it without complaint. It will probably cause car crashes, house fires, and seg faults, but nano-quack has no protections against any of that. (from Professor Michal)
Mini-Quack is the third project of CS 461. It includes control flow and type inference.
-
qklib/
: Contains the necessary parsing files for Quack.quack_grammar.txt
: Grammar Definition.quack_ast.py
: Build AST Nodes.quack_parser.py
: Parser implementation.quack_transformer.py
: AST transformation logic.quack_type_inference.py
: Type inference logic.
-
samples/
: Contains sample test cases.BasicFunction/
: Test Cases for basic function testing.test1_Int.qk
: Int Assignment with Add, Sub Optest2_Float.qk'
: Float Assignment with Mul, Div Optest3_String.qk'
: String Assignment with Add Optest4_Call.qk'
: Method Call with Int, Float, String Typetest5_If.qk'
: Control Flow: If Statementtest6_While.qk'
: Control Flow: While Statement
TypeInference/
: Test Cases for Type Inference testing.test1_TypeOperations.qk
: Same Type Add/Sub/Mul/Div for Int, Float, Stringtest2_TypeMismatch.qk'
: Error of TypeMismatchtest3_VarNotdefined.qk'
: Error of VarNotDefinedtest4_Boolean.qk'
: Boolean Assignment, Boolean Type for logic Optest5_functions.qk'
: Function Declaration
blahblahblah.qk
: original test codeformals.qk
: original test codeifelse
: original test code
-
OBJ/
: Contains generated assembly or object files. -
quack.sh
: Script to compile and run a Quack program. -
quackc.sh
: Script to compile a Quack program without running it.
I add the code generation file to generate assembly code. Now it can be tested with a simple quack program about Integer Assignment.
To Test the Code:
python3 qklib/quack_parser.py samples/CodeGeneration/add.qk
The Quack code is:
a: Int = 15;
b: Int = 9;
c: Int = a + b;
d: Int = c - 5;
The Assembly Tree generate by the Grammar is:
AST Tree: Block([Assign(a, Int, Int(15)), Assign(b, Int, Int(9)), Assign(c, Int, Add(Var(a), Var(b))), Assign(d, Int, Sub(Var(c), Int(5)))])
The target code generation is:
Code Generation:
alloc 4
const 15
store 0
const 9
store 1
load 0
load 1
call Int: plus
store 2
load 2
const 5
call Int: minus
store 3
You must install lark before running. You can install it in a virtual environment as follows:
python3 -m venv env
. env/bin/activate
pip3 install -r requirements.txt
The second command above (with activate
)
makes the virtual environment active.
To run:
python3 quack_front.py samples/blahblahblah.qk
The output should be a poorly indented
version of blahblahblah.qk with some
"normalization", e.g., the constructor
methods are included as if they had been
enclosed in a method declaration. Following
that you should find a symbol table
consisting of the contents of
builtin_methods.json
and a similar
record for the method Point
from
blahblahblah.
To leave the virtual environment after running the sample, you can use this command:
deactivate