Implementing a Scanner and Parser for a Simple Language

Please watch the attached pdf “PLDA.pdf” for problem and refer the “sample_run.txt” file and “reference codes” for reference.

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper

A compiler converts a source language program into a target language program. There are
some basic stages in the 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. Your task is to implement a scanner and a parser for this simple
language. Let’s name our language Blazer with .bl as the program file extension.
Context-Free Grammar:
->{}’halt’
-> 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 halt.
INT_CONSTANT
-> digit digit*
LETTER
->[a-zA-Z]
DIGIT
->[0-9]
Notes:
1. * means repeat zero or more times.
2. You may find strcmp useful for implementing reserved words/keywords in a language;
Google it for concrete examples.
3. Programs can be read from a file or keyboard, but files are recommended.
4. Sample runs will be provided later.
Submission notes
1. All your source code
2. Two screen shots–one for sample0.bl, the other for sample1.bl.
3. 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. Any reflection/comments on this assignment.
Script started on Tue Sep 19 10:20:25 EDT 2023
Programming Languages, Fall 23
Output for Homewrok 2
Program sample0.bl:
read a
write a
halt
starting the parsing process…
Call lex…read
Enter
Enter
Call lex…a
Call lex…write
Exit
Enter
Call lex…a
Enter
Enter
Enter
Call lex…halt
Exit
Exit
Exit
Exit
Call lex…EOF
Exit
This is a valid program.
Program sample1.bl:
read a
read b
result = (a + b)/2
write result
halt
starting the parsing process…
Call lex…read
Enter
Enter
Call lex…a
Call lex…read
Exit
Enter
Call lex…b
Call lex…result
Exit
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
Call lex…result
Enter
Enter
Enter
Call lex…halt
Exit
Exit
Exit
Exit
Call lex…EOF
Exit
This is a valid program.
Program sample2.bl:
read a
read b
result = (a+b/2
write result
halt
starting the parsing process…
Call lex…read
Enter
Enter
Call lex…a
Call lex…read
Exit
Enter
Call lex…b
Call lex…result
Exit
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.
This is an invalid program
Program sample3.bl:
read a
read b
result = (a + b) * 2
write result
starting the parsing process…
Call lex…read
Enter
Enter
Call lex…a
Call lex…read
Exit
Enter
Call lex…b
Call lex…result
Exit
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
Call lex…result
Enter
Enter
Enter
Call lex…EOF
Exit
Exit
Exit
Exit
ERROR: NO ‘halt’ STATEMENT.
This is an invalid program
Program sample4.bl:
read a
read b
result = ( a + b) 2
write result
halt
starting the parsing process…
Call lex…read
Enter
Enter
Call lex…a
Call lex…read
Exit
Enter
Call lex…b
Call lex…result
Exit
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
ERROR: Invalid statement.
This is an invalid program
Script done on Tue Sep 19 10:20:25 EDT 2023
Sample0.b1 : code in this file is mentioned below
read a
write a
halt
Sample1.b1 : code in this file is mentioned below
read a
read b
result = (a + b)/2
write result
halt
Sample2.b1: code in this file is mentioned below
read a
read b
result = (a+b/2
write result
halt
Sample3.b1: code in the file is mentioned below
read a
read b
result = (a + b) * 2
write result
Sample4.b1: code in the this file is mentioned below
read a
read b
result = ( a + b) 2
write result
halt

Order a unique copy of this paper

600 words
We'll send you the first draft for approval by September 11, 2018 at 10:52 AM
Total price:
$26
Top Academic Writers Ready to Help
with Your Research Proposal

Order your essay today and save 25% with the discount code GREEN