Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- public class Parser {
- private Lexer lex;
- private BufferedReader pbr;
- private Token look;
- public Parser(Lexer l, BufferedReader br) {
- lex = l;
- pbr = br;
- move();
- }
- void move() {
- look = lex.lexical_scan(pbr);
- System.out.println("token = " + look);
- }
- void error(String s) {
- throw new Error("near line " + lex.line + ": " + s);
- }
- void match(int t) {
- if (look.tag == t) {
- if (look.tag != Tag.EOF) move();
- } else error("syntax error");
- }
- public void prog() {
- if(look.tag == Tag.ID || look.tag == Tag.PRINT || look.tag == Tag.READ || look.tag == Tag.IF || look.tag == Tag.FOR || look.tag == Tag.BEGIN || look.tag == Tag.ELSE) {
- statlist();
- match(Tag.EOF);
- } else error("syntax (prog)");
- }
- public void statlist() {
- if(look.tag == Tag.ID || look.tag == Tag.PRINT || look.tag == Tag.READ || look.tag == Tag.IF || look.tag == Tag.FOR || look.tag == Tag.BEGIN || look.tag == Tag.ELSE) {
- stat();
- statlistp();
- } else error("syntax (statlist)");
- }
- public void stat() {
- if(look.tag == Tag.ID || look.tag == Tag.PRINT || look.tag == Tag.READ || look.tag == Tag.IF || look.tag == Tag.FOR || look.tag == Tag.BEGIN) {
- switch(look.tag) {
- case Tag.ID:
- match(Tag.ID);
- match('=');
- expr(); //modifica
- break;
- case Tag.PRINT:
- match(Tag.PRINT);
- match('(');
- expr();
- match(')');
- break;
- case Tag.READ:
- match(Tag.READ);
- match('(');
- match(Tag.ID);
- match(')');
- break;
- case Tag.IF:
- match(Tag.IF);
- bexpr();
- statelse();
- break;
- case Tag.FOR:
- match(Tag.FOR);
- match('(');
- match(Tag.ID);
- match('=');
- match(';');
- bexpr();
- match(')');
- match(Tag.DO);
- stat();
- break;
- case Tag.BEGIN:
- match(Tag.BEGIN);
- statlist();
- match(Tag.END);
- break;
- }
- } else error("syntax (stat)");
- }
- public void statlistp() {
- if(look.tag == ';' || look.tag == Tag.END || look.tag == Tag.EOF) {
- switch(look.tag) {
- case ';':
- match(';');
- stat();
- statlistp();
- break;
- default:
- break;
- }
- } else error("syntax (statlistp)");
- }
- public void bexpr() {
- if(look.tag == '(' || look.tag == Tag.NUM || look.tag == Tag.ID) {
- expr();
- match(Tag.RELOP);
- expr();
- } else error("syntax (bexpr)");
- }
- public void statelse() {
- if(look.tag == Tag.ELSE || look.tag == ';' || look.tag == Tag.END || look.tag == Tag.EOF) {
- switch(look.tag) {
- case Tag.ELSE:
- match(Tag.ELSE);
- stat();
- break;
- default:
- break;
- }
- } else error("syntax (statelse)");
- }
- private void expr() {
- if(look.tag == '(' || look.tag == Tag.NUM || look.tag == Tag.ID) {
- term();
- exprp();
- } else error("Syntax error (expr)");
- }
- private void exprp() {
- switch (look.tag) {
- case '+':
- match('+');
- term();
- exprp();
- break;
- case '-':
- match('-');
- term();
- exprp();
- break;
- case ')':
- case Tag.EOF:
- case Tag.RELOP:
- case Tag.ELSE:
- case Tag.END:
- case ';':
- break;
- default:
- error("Syntax error (exprp)");
- }
- }
- private void term() {
- if(look.tag == '(' || look.tag == Tag.NUM || look.tag == Tag.ID) {
- fact();
- termp();
- } else error("Syntax error (term)");
- }
- private void termp() {
- switch(look.tag) {
- case '*':
- match('*');
- fact();
- termp();
- break;
- case '/':
- match('/');
- fact();
- termp();
- break;
- case '+':
- case '-':
- case Tag.EOF:
- case ')':
- case Tag.RELOP:
- case Tag.ELSE:
- case Tag.END:
- case ';':
- break;
- default:
- error("Syntax error (termp)");
- }
- }
- private void fact() {
- switch(look.tag) {
- case '(':
- match('(');
- expr();
- match(')');
- break;
- case Tag.NUM:
- match(Tag.NUM);
- break;
- case Tag.ID:
- match(Tag.ID);
- break;
- default :
- error("Syntax error (fact)");
- }
- // ... completare ...
- }
- public static void main(String[] args) {
- Lexer lex = new Lexer();
- String path = "input.txt"; // il percorso del file da leggere
- try {
- BufferedReader br = new BufferedReader(new FileReader(path));
- Parser parser = new Parser(lex, br);
- parser.prog();
- System.out.println("Input OK");
- br.close();
- } catch (IOException e) {e.printStackTrace();}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement