Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CS 152 Project Outline

Introduction

The project should demonstrate some form of parsing capabilities using antlr4. You will need to build an application.

1. Your application must accept input

Input can be just a single line, you may use multi lines, files or even a running application accepting input parameters.

2. Your application must have a grammar

A grammar should contain lexer and parser rules and must solve an actual problem. Can be some part of a programming language. Can be a new language completely.

3. Your application must have an evaluation model

Once you enter a statement to your application, it should evaluate to something meaningful. It means that even if something is grammatically valid, it might not be semantically correct. Your application should handle that.

4. Your application must use antlr4, you may choose a language of your choice, but python would be preferred.

Evaluation Criteria

1.   Your code must be running, accept input and show meaningful results. If something is not following the grammar should print the errors as compilation errors. If something is not    semantically correct should print errors as logical/runtime errors.

2.   The complexity of the grammar will carry a significant weight.

3.   The complexity of the evaluation model will also carry a significant weight.

Example Project

Build a sql like interpreter that accepts the following commands:

// Creates a table student with name and age being two columns.

CREATE table Student(name : string, age : int);

// Adds a row to the table

INSERT INTO Student("alice", 12);

// Shows all records saved in the table

SELECT name, age FROM Student;

// Supports basic filter with conditionals

SELECT name FROM Student

WHERE name != "alice" OR age = 15;

1. Your main function might look like this (a command line app) which reads sql statements from a file:

def main(argv):

# Takes input from a file

input = InputStream(readInputFile("test.txt"))

lexer = ExprLexer(input)

stream = CommonTokenStream(lexer)

parser = ExprParser(stream)

tree = parser.prog()

 

# MySqlVisitor extends the generated visitor file

MySqlVisitor().visitProg(tree)  # Evaluate the expression

 

if __name__ == '__main__':

main(sys.argv)

2. Your grammar for the given statements might look like this:

grammar Expr;

prog: expr* EOF;

expr: create_stmt

| select_stmt

| insert_stmt

;

create_stmt: 'CREATE' 'table' ID '(' column_list ')' ;

column_list: column_spec ( ',' column_spec )* ;

column_spec: ID ':' column_type;

Column_type: 'string' | 'int';

 

...

NEWLINE : [\r\n]+ ;

INT     : [0-9]+ ;

WS      : [ \t\r\n] -> channel(HIDDEN);

3. Once you run your grammar through antlr, antlr will generate lexer, parser, visitor, listeners, etc files for you. At this point, invalid statements should result in errors, i.e 'INSERT Student(..)' should

throw an error (INTO keyword is missing). But the following statement should also result in errors:

CREATE table Student(name : string, age : int);

// This should throw an error as student table only has two columns,

// even though its grammatically correct

INSERT INTO Student("alice", 12, 50);

Now, once we run through the following statements:

CREATE table Student(name : string, age : int);

INSERT INTO Student("alice", 12);

INSERT INTO Student("bob", 15);

INSERT INTO Student("cindy", 16);

SELECT age, name FROM Student

WHERE name != "alice" OR age = 16;

It should print the following:

bob, 15

cindy, 16

MysqlVistor file might look like this:

class MySqlVisitor(ExprVisitor):

def __init__(self):

self.stack = []

Self.tables = {}

def visitProg(self, ctx:ExprParser.ProgContext):

return self.visit(ctx.expr())  # Just visit the self expression

def visitCreateExpr(self, ctx:ExprParser.CreateExprContext):

if ctx.ID() in self.tables:

raise ValueError(f"table {ctx.ID()} already exists")

// Save the table definition in the dictionary

...