CS320- Assignment 2 Dr. Sara Farag 1 CS320 Programming Languages Assignment 2 Due: February 29, 2020 at 11:59 PM Objectives In your future career some of the common tasks that you might be involved in...

File attached


CS320- Assignment 2 Dr. Sara Farag 1 CS320 Programming Languages Assignment 2 Due: February 29, 2020 at 11:59 PM Objectives In your future career some of the common tasks that you might be involved in are to analyze logs, create utilities for generating images from text descriptions, working in a project that involve Natural Language Processing NLP or write interpreters and compilers for new programming languages, and some others. These are all applications that will require you to write a parser for a file based on a specific structure to complete the task. This can be a tedious task, but fortunately there are more efficient ways to auto generate a parser. By completing this assignment, you will practice how to write a grammar that parse a file based on a defined syntax structure. You will practice the use of a popular parser and laxer tool (ANTLR) that is widely used by professionals. It auto generates a parser and a lexer in different languages based on your target language (e.g. Java, Python, C# and JavaScript). You can use the generated lexer and parser by invoking them in different applications. Therefore, for your projects that will need a parser, you don’t have to write that code and just focus your efforts on having a correct grammar file. Skills you gain after completing this assignment: • You will understand how different programming language detect the syntax errors • You will learn how to write a grammar for a file based on defined syntax structure • You will practice how to use ANTLR parser generator that helps you to create parsers. Tasks You are given the following Program.txt file (download the file). This file has a sample code for new programming Languages, the code is trying to do some mathematical calculation for a function parameter using particular programming syntax structure. Please write a Grammar using ANTLR4 for parsing the code and its tokens. //Program.txt. add <- function(x,="" y)="" {="" return(x="" +="" y)="" }="" subtract=""><- function(x,="" y)="" {="" return(x="" -="" y)="" }="" multiply=""><- function(x,="" y)="" {="" return(x="" *="" y)="" }="" divide=""><- function(x,="" y)="" {="" return(x="" y)="" }="" countdown=""><- function(from)="" {="" print(from)="" while(from!="0)" {="" sys.sleep(1)="" from=""><- from="" -="" 1="" print(from)="" }="" }="" https://bc.instructure.com/courses/1863889/files/folder/assignments/assignment%202%202020?preview="131869214" cs320-="" assignment="" 2="" dr.="" sara="" farag="" 2="" the="" following="" figures="" (in="" page="" 3)="" shows="" the="" expected="" output="" of="" a="" parse="" tree="" for="" the="" given="" program="" format.="" it="" also="" can="" be="" accessed="" as="" .pdf="" and="" .svg="" file="" where="" you="" can="" zoom="" in="" and="" out="" for="" readability.="" please="" note:="" your="" grammar="" needs="" to="" parse="" the="" file="" same="" way="" as="" it="" has="" been="" given="" to="" you.="" don’t="" change="" the="" .txt="" file="" formats="" what="" to="" submit="" 1.="" please="" submit="" your="" grammar="" as="" a="" g4="" file="" via="" canvas="" and="" 2.="" provide="" a="" sample="" screenshot="" for="" the="" parse="" tree="" you="" generated="" via="" canvas.="" how="" it="" will="" be="" graded="" to="" get="" a="" full="" grade="" you="" must="" provide="" a="" correct="" grammar="" that="" generate="" an="" ast="" same="" as="" the="" provided="" one.="" your="" grammar="" shouldn’t="" be="" ambiguous.="" for="" every="" missing="" or="" incorrect="" define="" rule="" some="" points="" will="" be="" deducted.="" useful="" tips="" 1.="" remember="" antlr="" resolves="" an="" ambiguity="" by="" choosing="" the="" first="" alternative="" in="" the="" grammar,="" so="" the="" order="" of="" the="" rules="" does="" matter.="" 2.="" steps="" to="" run="" antrl="" and="" have="" the="" gui="" parse="" tree="" a.="" in="" your="" cmd="" wind="" navigate="" to="" the="" file="" where="" you="" have="" the="" grammar="" (.g4)="" file="" and="" the="" program.txt="" b.="" use="" the="" antlr4="" program="" to="" generate="" the="" files="" that="" your="" program="" will="" actually="" use,="" such="" as="" the="" lexer="" and="" the="" parser="" antlr4=""> c. compile lexer and parser javac *.java d. You can optionally test your grammar using a little utility named TestRig (although, as we have seen, it’s usually aliased to grun). grun Example ANTLR Grammar File You probably noticed that there’s a mix of lower- and upper-case words in the grammar. Lower cased words are parser expressions, UPPER CASE words are lexer expressions. It is important to understand that the whole parsing is happening in two phases; 1. the lexer creates the token stream and the parser 2. then builds the AST from this token stream. If your grammar is ambiguous the lexer might interpret a token wrong and then you’ll see the parser complain about receiving an unexpected token while in fact it was the lexing stage where this went wrong. grammar Expr; /** The start rule; begin parsing here. */ prog: stat+ ; stat: expr NEWLINE | ID '=' expr NEWLINE | NEWLINE ; expr: expr ('*'|'/') expr | expr ('+'|'-') expr | INT | ID | '(' expr ')' ; ID : [a-zA-Z]+ ; // match identifiers
Feb 24, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here