We have to program it in two parts: the lexical analyzer and the syntax analyzer plus semantic.The lexical analyzer should recognize the token using DFA and return the token and the value. The syntax and semantic part can be done by using any algorithm that can check for the correct syntax.You can select one programming language as the source language to build compiler to it or construct your own ones or take the one I write it in the appendix of this assignment (source program).The language that will be used as source program (calculator) will contain only the operations add, subtract, multiply, divide, power and minus unary operations and two types of variables, integer and real and also space has to be used to separate between tokens.
The following is Syntax structure for a language that works as calculator. Any words in bold mean it is terminal
Prog -> Header Declar Statm
Header -> program ( id) ; id is name for the program
Declar -> D Declar |D
D -> id: Type ;
Type -> real | integer
Statm -> [ S ]
S -> Exp1 ; S |Exp1 ;
Exp1 -> id = Exp
Exp -> Exp + Exp | Exp – Exp | Exp * Exp | Exp / Exp | Exp ^ Exp |_ Exp| (Exp) | id | num _ is unary minus operation
^ Power operation
Code : (java)
import java.io.*;
public class readfile {
/**
* Creates a new instance of <code>readfil e</code>.
*/
public readfile() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
FileInputStream myFile = null;
try {
myFile = new FileInputStream ("info.txt") ; // open the stream
BufferedWriter out = new BufferedWriter( new FileWriter("new text.txt")); // open writing stream
boolean eof = false;
while (!eof) {
int byteValue = myFile.read(); // read the stream
char c = (char)byteValue ;
c = Character.toLow erCase(c);
System.out.prin tln(c + " ");
out.write(c);
if (byteValue == -1) //if it is at the end of file
eof = true;
}
out.close();
} catch (IOException e) {
System.out.prin tln("Could not read file: " + e.toString());
} finally{
try{
myFile.close(); // close the stream
} catch (Exception e1){
e1.printStackTr ace();
}
}
}
}
The following is Syntax structure for a language that works as calculator. Any words in bold mean it is terminal
Prog -> Header Declar Statm
Header -> program ( id) ; id is name for the program
Declar -> D Declar |D
D -> id: Type ;
Type -> real | integer
Statm -> [ S ]
S -> Exp1 ; S |Exp1 ;
Exp1 -> id = Exp
Exp -> Exp + Exp | Exp – Exp | Exp * Exp | Exp / Exp | Exp ^ Exp |_ Exp| (Exp) | id | num _ is unary minus operation
^ Power operation
Code : (java)
import java.io.*;
public class readfile {
/**
* Creates a new instance of <code>readfil e</code>.
*/
public readfile() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
FileInputStream myFile = null;
try {
myFile = new FileInputStream ("info.txt") ; // open the stream
BufferedWriter out = new BufferedWriter( new FileWriter("new text.txt")); // open writing stream
boolean eof = false;
while (!eof) {
int byteValue = myFile.read(); // read the stream
char c = (char)byteValue ;
c = Character.toLow erCase(c);
System.out.prin tln(c + " ");
out.write(c);
if (byteValue == -1) //if it is at the end of file
eof = true;
}
out.close();
} catch (IOException e) {
System.out.prin tln("Could not read file: " + e.toString());
} finally{
try{
myFile.close(); // close the stream
} catch (Exception e1){
e1.printStackTr ace();
}
}
}
}
Comment