Made by @dguido1, @TimothyBui, @imjoshy & @BradK14
at California State University Fullerton
Fall 19'
- Inputs a text file with 'program code'
- Removes extra white space & comments
- Outputs an intermediate code translation in the form of a C++ program
-
Have a program that
- Reads in the text file
final.txt
- Formats it properly
- Outputs the result to a second text file
revised.txt
- Command make strip will take final.txt and strip it of whitespace and comments
- Reads in the text file
-
Specifies production rules using Flex/Bison
-
Translates the program into a high level programming language (C++)
-
Command for Flex/Bison make parse will take the new revised.txt file, parse it, and convert it into working C++ code
-
Runs
abc13.cpp
using the C++ compiler to the correct output
Suppose that the context of the text file final.txt
is
(* Program Name: Spring 2014
Final Project
Name: Nathan Reed
---------------------------------------------------*)
PROGRAM aba13;
VAR
ab5, cb , be, eb: INTEGER; (* declare variables *)
BEGIN
ab5 = 5 ;
cb =10;
PRINT(‘ab5=’, ab5);
(* compute and print the total of a and c *)
eb= cb + ab5 ;
PRINT(‘eb=’ ,eb ) ;
be - 2*abc5 + eb;
PRINT( be ) ;
END.
- You are going to use this program to check the grammar of each statement
- The program should look like the following
PROGRAM aba13;
VAR
ab5, cb, be, eb : INTEGER;
BEGIN
ab5 = 5;
cb = 10;
PRINT(‘ab5=’, ab5);
cb = cb + ab5;
PRINT( cb );
be = 2 * ab5 + eb;
PRINT( be );
END.
- Given the following CFG (some rules are in EBNF form)
<start> →PROGRAM<pname>; VAR ,<dec-list>; BEGIN <stat-list> END.
<pname> → <id>
<id> → <letter>{<letter>|<digit>}
<dec-list> → <dec> : <type>
<dec> → <id>, <dec> | <id>
<stat-list> → <stat>; | <stat>; <stat-list>
<stat> → <print> | <assign>
<print> → PRINT (<output>)
<output> → [“string”,} <id>
<assign> → <id> = <expr>
<expr> → <term> | <expr> + <term> | <expr> - <term>
<term> → <term> * <factor> | <term> / <factor> | <factor>
<factor> → <id> | <number> | <( <expr> )
<number> → <digit>{<digit>}
<type> → INTEGER
<digit> → 0|1|2|…|9
<letter> → a|b|c|d|e|f
- Where PROGRAM, VAR, BEGIN, END. , INTEGER, PRINT are reserved words, and all uppercase.
- Flex/bison should print out the errors, but MANUALLY fix the errors
- Your job is to use the given grammar and determine whether the program is accepted or not
- i.e. Whether there are or aren’t syntax errors in the program.
- Your program should produce one of the following error messages as soon as the error is detected
PROGRAM is expected (if PROGRAM is missing or spelled wrong)
VAR is expected (if VAR is missing or spelled wrong)
BEGIN is expected (if BEGIN is missing ir spelled wrong)
END. is expected (if END. is missing or spelled wrong)
UNKNOWN IDENTIFIER if variable is not defined
; ; is missing
‘ ‘ is missing
. . is missing
( ( is missing
) ) is missing
, , is missing
= = is missing
3. If there are no errors in the program, translate the program into a C++/or another high level programming language
-
Store the C++ program as
abc13.cpp
-
Your C++ program should look like this
#include <iostream>
Using namespace std;
int main()
{
int ab5, cb, be, eb;
ab5 = 5
cout << ”ab5=” << ab5;
eb = cb + ab5;
cout << eb;
be = 2 * ab5 + eb;
cout << be;
return 0;
}