subject

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

ansver
Answers: 2

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 00:30
Advantages and disadvantages of binary system
Answers: 1
question
Computers and Technology, 22.06.2019 14:20
Consider a byte-addressable computer with 16mb of main memory, a cache capable of storing a total of 64kb of data and block size of 32 bytes. (a) how many bits in the memory address? (b) how many blocks are in the cache? (c) specify the format of the memory address, including names and sizes, when the cache is: 1. direct-mapped 2. 4-way set associative 3. fully associative
Answers: 2
question
Computers and Technology, 22.06.2019 17:00
The two main ways in which marketers address the competition with their strategies are by satisfying a need better than a competition and by
Answers: 2
question
Computers and Technology, 23.06.2019 03:00
Jason, samantha, ravi, sheila, and ankit are preparing for an upcoming marathon. each day of the week, they run a certain number of miles and write them into a notebook. at the end of the week, they would like to know the number of miles run each day, the total miles for the week, and average miles run each day. write a program to them analyze their data. your program must contain parallel arrays: an array to store the names of the runners and a two-dimensional array of five rows and seven columns to store the number of miles run by each runner each day. furthermore, your program must contain at least the following functions: a function to read and store the runners’ names and the numbers of miles run each day; a function to find the total miles run by each runner and the average number of miles run each day; and a function to output the results. (you may assume that the input data is stored in a file and each line of data is in the following form: runnername milesday1 milesday2 milesday3 milesday4 milesday5 milesday6 milesday7.)
Answers: 3
You know the right answer?
Implementing a Scanner and Parser for a Simple Language A compiler converts a source language prog...
Questions
question
Mathematics, 23.07.2019 10:30
question
Computers and Technology, 23.07.2019 10:30
Questions on the website: 13722360