Question: Write a context free grammar (BNF) You will be writing a concrete grammar for the statement portion of Gee. This is intended as an implementation

Write a context free grammar (BNF)

You will be writing a concrete grammar for the statement portion of Gee. This is intended as an implementation grammar. You are to design and write a grammar that accepts programs in the Gee programming language (written below). The grammar should be unambiguous (except for the if) and may be specified in either EBNF or BNF.

Be sure that it's clear in your grammar which symbols are terminal symbols and which are non-terminal symbols. E.g.: use boldface for terminal symbols and italics for non-terminal symbols, or some other format you prefer. Please specify at the top of your document what format you using.

Gee Programming Language

A Gee program is made up of one or more statements in a Python-like syntax:

i = 0 m = MyClass( ) // Java: MyClass m = new MyClass() print("Hello, World!") 

A statement must be one of the following:

An assignment statement of the form:

var = expression 

A loop statement:

while i < 5: sum = sum + i i = i - 1 print(sum) 

In this case, both assignment statements are in the loop. The scanner issues an indent between the ":" and the "sum", and an undent between the "1" and the "print".

An if statement:

if a == b: max = a else: print( "min =", c) 

An if statement may have an optional else. There may be arbitrarily many statements in the then, and else parts of an if.

A function call.

printHistogram(data*3+x, xaxis, yaxis)

Assumptions

You may assume the presence of a "scanner" that takes an input file and turns it into a sequence of tokens, which would then need to be matched against your grammar. The alphabet of your grammar the set of possible tokens should include the following special tokens, in addition to the literal ones such as parentheses and the operators:

IntLiteral, which represents an integer literal.

StringLiteral, which represents a string literal.

Identifier, which represents an identifier.

Indent, which indicates the indentation level increased.

Undent, which indicates the indentation level decreased.

Eoln, which indicates an end-of-line.

As an example, consider the Gee loop program:

while i < 5: sum = sum + i i = i - 1 print, (, sum, ), Eoln 

It would be scanned and turned into the following token sequence: assuming that an Expression was treated as a Terminal Symbol; ie., your grammar need only cover statements, not expressions.

while, Expression, :, Eoln, Indent, Identifier, =, Expression, Eoln Identifier, =, Expression, Eoln Undent Identifier, (, Expression, ), Eoln 

Note, in particular, that indent/undent's appear only after an end-of-line (Eoln) and only if the indentation level changes. There is an Undent for each change in level.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!