Created
April 19, 2021 09:41
-
-
Save PwnVerse/158016ea961f52156f992ca134549380 to your computer and use it in GitHub Desktop.
Compiler RD Parser Lab exam
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.io.*; | |
import java.lang.*; | |
/* | |
* | |
* program -> prog block | |
* block -> begin stmtlist end | |
* stmtlist -> stmt morestmt | |
* morestmt -> ; stmtlist | є | |
* stmt -> assign | repeatstmt | |
* assign -> var = expr | |
* repeatstmt -> repeat stmt until testexpt | |
* testexpr -> var <= expr | |
* expr -> + expr expr | |
* | -expr expr | |
* | var | |
* |digit | |
* var -> u | v| w | |
* digit -> 0 | 1 |2 | |
*/ | |
// prog begin end repeat until -> treated as terminals | |
class Main{ | |
static int index = -1; | |
static String[] st; | |
static boolean match(String s){ | |
return s.equals(st[index++]); | |
} | |
static boolean program(){ | |
int back = index; | |
if(match("prog") && block()) | |
return true; | |
index = back; | |
return false; | |
} | |
static boolean block(){ | |
int back = index; | |
if(match("begin") && stmtlist() && match("end")) | |
return true; | |
index = back; | |
return false; | |
} | |
static boolean stmtlist(){ | |
int back = index; | |
if(stmt() && morestmt()) | |
return true; | |
index = back; | |
return false; | |
} | |
static boolean morestmt(){ | |
int back=index; | |
if(match(";") && stmtlist()){ | |
return true; | |
} | |
index = back; | |
return true; | |
} | |
static boolean stmt(){ | |
int back = index; | |
if(assign()) | |
return true; | |
else{ | |
index = back; | |
if(repeatstmt()) | |
return true; | |
} | |
index = back; | |
return false; | |
} | |
static boolean assign(){ | |
int back = index; | |
if(var() && match("=") && expr()) | |
return true; | |
index = back; | |
return false; | |
} | |
static boolean repeatstmt(){ | |
int back = index; | |
if(match("repeat") && stmt() && match("until") && testexpr()) | |
return true; | |
index = back; | |
return false; | |
} | |
static boolean testexpr(){ | |
int back = index; | |
if(var() && match("<=") && expr()) | |
return true; | |
index = back; | |
return false; | |
} | |
static boolean expr(){ | |
int back = index; | |
if(match("+") && expr() && expr()) | |
return true; | |
else{ | |
index = back; | |
if(match("-") && expr() && expr()) | |
return true; | |
else{ | |
index = back; | |
if(var()) | |
return true; | |
else{ | |
index = back; | |
if(digit()) | |
return true; | |
} | |
} | |
} | |
index = back; | |
return false; | |
} | |
static boolean var(){ | |
int back = index; | |
if(match("u")) | |
return true; | |
else{ | |
index = back; | |
if(match("v")) | |
return true; | |
else{ | |
index = back; | |
if(match("w")) | |
return true; | |
} | |
} | |
index = back; | |
return false; | |
} | |
static boolean digit(){ | |
int back = index; | |
if(match("0")) | |
return true; | |
else{ | |
index = back; | |
if(match("1")) | |
return true; | |
else{ | |
index = back; | |
if(match("2")) | |
return true; | |
} | |
} | |
index = back; | |
return false; | |
} | |
public static void main(String []args){ | |
Scanner sc = new Scanner(System.in); | |
System.out.print("Enter a string : "); | |
String inp = sc.nextLine(); | |
st = inp.split(" "); | |
index=0; | |
if(program() && match("$")){ | |
System.out.println("Valid"); | |
} | |
else | |
System.out.println("Invalid"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment