Question: Consider this straight line programming language without any support for loops or conditional statements. A typical straight line program consists of several sentences separated

Consider this straight line programming language without any support for loops or conditional statements. A

Consider this straight line programming language without any support for loops or conditional statements. A typical straight line program consists of several sentences separated by semicolon (;). Three different kinds of sen- tences are supported: 1. Variable definitions: using keyword var we can define a variable, for example: var abc defines a variable with name abc. Only letters a...z and A... Z are allowed in variable names. 2. Assignments: using symbol := we can assign value of some expres- sion to a variable. Left hand side of this symbol is always a variable name and right hand side is always an expression. An expression is composition of variable names and natural numbers using arithmetic operations +,-, * and /. A single natural number or a single variable name is also an expression. Some samples: abc = -23 a: 2 v = 12 * v/abc- 4 v := uvy 3. Output statements: using keyword print we can print a variable value to the output. For example: print abc prints the value of variable abc to the output. The language is case sensitive and the only allowed characters are +, -, *, /, , , , a,... z, A,... Z, 0...9. Note that a program is valid if and only if it conforms to the previous defined rules for this language. Be careful not to add any rules by your intuition, for example we never required a variable name to be declared by the keyword var before being used, so for example this is a valid program: abc : =5; var Acd ; Acd=abc * 6* pqr; print fg Checking those kind of restrictions that are usually needed for serious programming languages requires more machinery. Design a context free grammar for this language (that is, construct a grammar G such that L(G) is the set of all valid programs in this language.) Do not worry about spaces or newlines for this grammar, you may assume keywords such as var and print are not variable names. PS5, Page 3 1. Define the rules digits (0 through 9), letters (lower case and up- percase alphabet), and operations op (-, +, * , /). 2. Define the rule for variable names varName using the definition letters (Note this is not variable declaration.) 3. Define the rule for numbers number using digits. 4. Define the rule for variable declaration varDec using varName. 5. Define the rule for expressions expression using op, number, and varName (remember an expression can just be a number or variable). 6. Define the rule for print statements print using varName 7. Define the rule for expressions assignment using varName and expression. 8. Define the rule for sentences sentence using varDec, assignment, and print. 9. Define the rule for programs prog using sentence.

Step by Step Solution

3.41 Rating (148 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

For the given straightline programming languages we are defining a contextfree grammerCFG 1Define the rules for digits 0 through 9 letters lower case ... View full answer

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 Programming Questions!