Advertisement
Guest User

Parser.java

a guest
Nov 23rd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.51 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. public class Parser {
  4.     private Lexer lex;
  5.     private BufferedReader pbr;
  6.     private Token look;
  7.  
  8.     public Parser(Lexer l, BufferedReader br) {
  9.         lex = l;
  10.         pbr = br;
  11.         move();
  12.     }
  13.  
  14.     void move() {
  15.         look = lex.lexical_scan(pbr);
  16.         System.out.println("token = " + look);
  17.     }
  18.  
  19.     void error(String s) {
  20.     throw new Error("near line " + lex.line + ": " + s);
  21.     }
  22.  
  23.     void match(int t) {
  24.     if (look.tag == t) {
  25.         if (look.tag != Tag.EOF) move();
  26.     } else error("syntax error");
  27.     }
  28.    
  29.     public void prog() {
  30.     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) {
  31.         statlist();
  32.         match(Tag.EOF);
  33.     } else error("syntax (prog)");
  34.     }
  35.    
  36.     public void statlist() {
  37.     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) {
  38.         stat();
  39.         statlistp();
  40.     } else error("syntax (statlist)");
  41.     }    
  42.  
  43.     public void stat() {
  44.     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) {
  45.         switch(look.tag) {
  46.             case Tag.ID:
  47.                 match(Tag.ID);
  48.                 match('=');
  49.                 expr(); //modifica
  50.                 break;
  51.  
  52.             case Tag.PRINT:
  53.                 match(Tag.PRINT);
  54.                 match('(');
  55.                 expr();
  56.                 match(')');
  57.                 break;
  58.  
  59.             case Tag.READ:
  60.                 match(Tag.READ);
  61.                 match('(');
  62.                 match(Tag.ID);
  63.                 match(')');
  64.                 break;
  65.  
  66.             case Tag.IF:
  67.                 match(Tag.IF);
  68.                 bexpr();
  69.                 statelse();
  70.                 break;
  71.        
  72.             case Tag.FOR:
  73.                 match(Tag.FOR);
  74.                 match('(');
  75.                 match(Tag.ID);
  76.                 match('=');
  77.                 match(';');
  78.                 bexpr();
  79.                 match(')');
  80.                 match(Tag.DO);
  81.                 stat();
  82.                 break;
  83.            
  84.             case Tag.BEGIN:
  85.                 match(Tag.BEGIN);
  86.                 statlist();
  87.                 match(Tag.END);
  88.                 break; 
  89.         }
  90.     } else error("syntax (stat)");
  91.     }    
  92.    
  93.     public void statlistp() {
  94.         if(look.tag == ';' || look.tag == Tag.END || look.tag == Tag.EOF) {
  95.         switch(look.tag) {
  96.             case ';':
  97.                 match(';');
  98.                 stat();
  99.                 statlistp();
  100.                 break;
  101.             default:   
  102.                 break; 
  103.         }
  104.     } else error("syntax (statlistp)");
  105.     }
  106.  
  107.     public void bexpr() {
  108.     if(look.tag == '(' || look.tag == Tag.NUM || look.tag == Tag.ID) {
  109.         expr();
  110.         match(Tag.RELOP);
  111.         expr();
  112.     } else error("syntax (bexpr)");
  113.     }
  114.  
  115.     public void statelse() {
  116.     if(look.tag == Tag.ELSE || look.tag == ';' || look.tag == Tag.END || look.tag == Tag.EOF) {
  117.         switch(look.tag) {
  118.             case Tag.ELSE:
  119.                 match(Tag.ELSE);
  120.                 stat();
  121.                 break; 
  122.  
  123.             default:
  124.                 break;
  125.         }
  126.     } else error("syntax (statelse)");
  127.     }
  128.  
  129.     private void expr() {
  130.     if(look.tag == '(' || look.tag == Tag.NUM || look.tag == Tag.ID) {
  131.         term();
  132.         exprp();
  133.     } else error("Syntax error (expr)");
  134.     }
  135.  
  136.     private void exprp() {
  137.     switch (look.tag) {
  138.         case '+':
  139.             match('+');
  140.             term();
  141.             exprp();
  142.             break;
  143.         case '-':
  144.             match('-');
  145.             term();
  146.             exprp();
  147.             break;
  148.         case ')':
  149.         case Tag.EOF:
  150.         case Tag.RELOP:
  151.         case Tag.ELSE:
  152.         case Tag.END:
  153.         case ';':
  154.             break;
  155.         default:
  156.             error("Syntax error (exprp)");
  157.     }
  158.     }
  159.  
  160.     private void term() {
  161.     if(look.tag == '(' || look.tag == Tag.NUM || look.tag == Tag.ID) {
  162.         fact();
  163.         termp();
  164.     } else error("Syntax error (term)");
  165.     }
  166.  
  167.     private void termp() {
  168.     switch(look.tag) {
  169.         case '*':
  170.             match('*');
  171.             fact();
  172.             termp();
  173.             break;
  174.         case '/':
  175.             match('/');
  176.             fact();
  177.             termp();
  178.             break;
  179.         case '+':
  180.         case '-':
  181.         case Tag.EOF:
  182.         case ')':
  183.         case Tag.RELOP:
  184.         case Tag.ELSE:
  185.         case Tag.END:
  186.         case ';':
  187.             break;
  188.         default:
  189.             error("Syntax error (termp)");
  190.     }
  191.     }
  192.  
  193.     private void fact() {
  194.     switch(look.tag) {
  195.         case '(':
  196.             match('(');
  197.             expr();
  198.             match(')');
  199.             break;
  200.         case Tag.NUM:
  201.             match(Tag.NUM);
  202.             break;
  203.         case Tag.ID:
  204.             match(Tag.ID);
  205.             break;
  206.         default :
  207.             error("Syntax error (fact)");
  208.     }
  209.         // ... completare ...
  210.     }
  211.        
  212.     public static void main(String[] args) {
  213.         Lexer lex = new Lexer();
  214.         String path = "input.txt"; // il percorso del file da leggere
  215.         try {
  216.             BufferedReader br = new BufferedReader(new FileReader(path));
  217.             Parser parser = new Parser(lex, br);
  218.             parser.prog();
  219.             System.out.println("Input OK");
  220.             br.close();
  221.         } catch (IOException e) {e.printStackTrace();}
  222.     }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement