Computers and Technology

Implementing a Scanner and Parser for a Simple Language A compiler converts a source language program into a target language program. There are some basic stages in compilation process, including scanning, parsing, semantic analysis, and code generation. For this assignment, you will implement a scanner and parser for a small language with its grammar given in BNF notation. The language literals are enclosed with ' ' in the CFG below.
Context Free Grammar:
->'end'
->|Π„ note: this is equivalent to |
-> ID '='
|'read' ID
|'write'
-> {('+'|'-')}
-> {('*'|'/') }
-> ID
| INT_CONSTANT
|'('')'
The possible set of tokens, represented with regular expressions, includes:
ASSIGN -> =
PLUS -> +
MINUS -> -
TIMES -> *
DIV -> /
LPAREN ->(
RPAREN -> )
ID -> letter(letter|digit)*
Except read, write, and end.
INT_CONSTANT -> digit digit*
LETTER ->[a-zA-Z]
DIGIT ->[0-9]
Your task is to implement a scanner and a parser for this simple language.
follow the following :
1. You may use any language of your choice to write your program. Howeprogram must compile and run on pluto or vcl.
2. Programs can be read from keyboard instead of file.
3. Sample runs can be foun.
Note :
All your source code
Brief documentation or readme file (i. e. no more than a page), to include the following
a. Instructions on how to build and execute your program
b. Any difficulties you encountered in designing/writing your program
c. What you learn/find from or reflection of this assignment.
The output should be like this
Output for Assigment 2
File sample1 contains
read a
read b
result = (a + b)/2
write result
end
starting the process...
Call lex...read
Enter
Enter
Enter
Call lex...a
Call lex...read
Exit
Enter
Enter
Call lex...b
Call lex...result
Exit
Enter
Enter
Call lex...=
Call lex...(
Enter
Enter
Enter
Call lex...a
Enter
Enter
Enter
Call lex...+
Exit
Exit
Call lex...b
Enter
Enter
Call lex...)
Exit
Exit
Exit
Call lex.../
Exit
Call lex...2
Enter
Call lex...write
Exit
Exit
Exit
Exit
Enter
Enter
Call lex...result
Enter
Enter
Enter
Call lex...end
Exit
Exit
Exit
Exit
Enter
Exit
Exit
Exit
Exit
Exit
This is a valid program
File sample2 contains
read a
read b
result = (a+b/2
write result
end
starting the process...
Call lex...read
Enter
Enter
Enter
Call lex...a
Call lex...read
Exit
Enter
Enter
Call lex...b
Call lex...result
Exit
Enter
Enter
Call lex...=
Call lex...(
Enter
Enter
Enter
Call lex...a
Enter
Enter
Enter
Call lex...+
Exit
Exit
Call lex...b
Enter
Enter
Call lex.../
Exit
Call lex...2
Enter
Call lex...write
Exit
Exit
Exit
ERROR: No closing parentheses.
File sample3 contains
read a
read b
result = (a + b) * 2
write result
starting the process...
Call lex...read
Enter
Enter
Enter
Call lex...a
Call lex...read
Exit
Enter
Enter
Call lex...b
Call lex...result
Exit
Enter
Enter
Call lex...=
Call lex...(
Enter
Enter
Enter
Call lex...a
Enter
Enter
Enter
Call lex...+
Exit
Exit
Call lex...b
Enter
Enter
Call lex...)
Exit
Exit
Exit
Call lex...*
Exit
Call lex...2
Enter
Call lex...write
Exit
Exit
Exit
Exit
Enter
Enter
Call lex...result
Enter
Enter
Enter
Call lex...EOF
Exit
Exit
Exit
Exit
Enter
ERROR: no ending
File sample4 contains
read a
read b
result = ( a + b) 2
write result
end
starting the process...
Call lex...read
Enter
Enter
Enter
Call lex...a
Call lex...read
Exit
Enter
Enter
Call lex...b
Call lex...result
Exit
Enter
Enter
Call lex...=
Call lex...(
Enter
Enter
Enter
Call lex...a
Enter
Enter
Enter
Call lex...+
Exit
Exit
Call lex...b
Enter
Enter
Call lex...)
Exit
Exit
Exit
Call lex...2
Exit
Exit
Exit
Exit
Enter
Enter
ERROR: Invalid statement

answer
Answers: 2

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 15:00, 2alshawe201
The three logical operators used to write compound conditions are "and," "or," and "not." a: true b: false
Answers: 2
image
Computers and Technology, 22.06.2019 19:00, alyssashae1818
Which parts of a presentation should be the most general? a. introduction and conclusion b. introduction and outline c. outline and conclusion d. outline and body
Answers: 1
image
Computers and Technology, 24.06.2019 09:50, trenrain
Create a string list. 2. use console. readline() to collect values of firstname, lastname, street, city, state, zip, save them to list. 3. write a simple linq statement, call method uppercasewords() to change first letter to uppercase. 4. create a foreach statment to display the information. public static string uppercasewords(string value) { char[] array = value. tochararray(); if (array. length > = 1) { if (char. islower(array[0])) { array[0] = char. toupper(array[0]); } } for (int i = 1; i < array. length; i++) { if (array[i - 1] == ' ') { if (char. islower(array[i])) { array[i] = char. toupper(array[i]); } } } return new string(array);
Answers: 3
image
Computers and Technology, 24.06.2019 12:00, yagalneedshelp8338
Match the function to its purpose. fast worth 50pts.
Answers: 1
Do you know the correct answer?
Implementing a Scanner and Parser for a Simple Language A compiler converts a source language prog...

Questions in other subjects: