Question: in java jcup(Also related to java JFlex) code first this is A3.lex import java_cup.runtime.*; %% %implements java_cup.runtime.Scanner %type Symbol %function next_token %class A3Scanner %eofval{ return
in java jcup(Also related to java JFlex)
code first
this is A3.lex
import java_cup.runtime.*; %%
%implements java_cup.runtime.Scanner %type Symbol %function next_token %class A3Scanner %eofval{ return null; %eofval}
ID = [a-zA-Z][a-zA-Z0-9]* NUMBER = [0-9]+
%% "+" { return new Symbol(A3Symbol.PLUS); } "-" { return new Symbol(A3Symbol.MINUS); } "*" { return new Symbol(A3Symbol.TIMES); } "/" { return new Symbol(A3Symbol.DIVIDE); } {NUMBER} { return new Symbol(A3Symbol.NUMBER);} | {} . {}
this is A3.cup
import java.io.*;
/* Terminals (tokens returned by the scanner). */ terminal PLUS, MINUS, TIMES, DIVIDE, LPAREN, RPAREN; terminal Integer NUMBER;
/* Non terminals */ non terminal Integer expr;
/* Precedences */ precedence left PLUS, MINUS; precedence left TIMES, DIVIDE;
/* The grammar */ expr ::= expr PLUS expr {: :} | expr MINUS expr {: :} | expr TIMES expr {: :} | expr DIVIDE expr {: :} | LPAREN expr RPAREN {: :} | NUMBER:e {: :} ;
This is A3User.java
import java.io.*; class A3User{ public static void main (String[] args) throws Exception { File inputFile = new File ("A3.tiny") ; A3Parser parser = new A3Parser(new A3Scanner (new FileInputStream(inputFile))); Integer result =(Integer)parser.parse().value; FileWriter fw =new FileWriter(new File("A3.output ")); fw.write(" Number of methods : " + result.intValue()); fw.close(); } } This is A3.tiny
/** sahftrjhesj ew** **/ INT f(INT x, INT y ) BEGIN z:=x; z := x*x - y*y; RETURN x+z; END
INT MAIN f1() BEGIN REAL x; READ(x, "A41.input"); REAL y; READ(y, "A42.input"); INT z;
z:=0; IF (x!=y) z := f2(x,y) + f2(y,x); WRITE (z, "A4.output"); END
Here is the A3.bat you need to use in command(for windows)
java JLex.Main A3.lex java java_cup.Main -parser A3Parser -symbols A3Symbol A3.cup javac A3.lex.java javac A3Parser.java A3Symbol.java A3User.java java A3User
There is the question
i need to have the a3.out file to tell me how many mathods is used in the input file, but i cant even start the first step. i will add more grammer (like quote, keywords like REAL, WRITE)to the cup file in the future but for now, can you help me with the first stap please? it really drive me creazy, thanks.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
