/** * Parse the input file * * @return true if the input is a part of the language, false if it is not */ public boolean parse() { pTable = new ParseTable(); boolean notFound = true; curState = stack.peek(); Token next = null; int nextState; String name = ""; try { while (notFound) { next = scanner.scan(); nextState = pTable.getNextState(curState, next.getP()); if (nextState == 9999) { System.err.println("Error in parsing on line " + scanner.getLine()); return false; } // next state is negative, reduce else if (nextState < 0) { while (nextState < 0) { reduce(nextState); nextState = pTable.getNextState(curState, next.getP()); } shift(nextState, next.getP()); curState = nextState; } // next state is positive and not a goto, shift else if (nextState > 0) { // took out && nextState < 100 shift(nextState, next.getP()); curState = nextState; } // next state is accept (only happens in state 6) if (nextState == 6) { gen.reduce(0); return true; } if (curState == 9999) { System.err.println("Error state reached while parsing."); System.err.println("Error on line: " + scanner.getLine()); return false; } } } catch (java.io.IOException ie) { ie.printStackTrace(); } catch (SyntaxError ex) { System.err.println("Syntax Error on line " + scanner.getLine() + " while parsing."); return false; } catch (compiler.CScanner.MaxIDSizeError me) { System.err.println(me.getMessage()); return false; } return false; }
/** * This is the reduce implementation * * @param rule the rule to reduce by */ private void reduce(int rule) throws SyntaxError { // get the rule number to reduce by int num = Math.abs(rule); // this section is for generating code if (num == 27 || num == 28 || num == 29 || num == 30 || num == 31 || num == 32 || num == 33 || num == 37 || num == 39 || num == 41) { gen.reduce(num); } // popping empty from the stack, just push if (GrammarTable.grammar[num].length != 1) { // start popping and matching stuff int input = 0; for (int i = GrammarTable.grammar[num].length - 1; i > 0; i--) { stack.pop(); input = stack.pop(); if (GrammarTable.grammar[num][i] == input) { continue; } else { throw new SyntaxError("Syntax error in source file on line " + scanner.getLine()); } } } // push the LHS curState = stack.peek(); // push the left hand side stack.push(GrammarTable.getRule(num)[0]); curState = pTable.getNextState(curState, GrammarTable.getRule(num)[0]); stack.push(curState); }
public Parser(String fName) { // create reserved word list createReserveList(); gen = new Generator(fName); try { scanner = new CScanner(fName, gen); read = new BufferedReader(new FileReader(fName + ".txt")); } catch (compiler.CScanner.EndOfFileException e) { System.err.println(e.getMessage()); e.printStackTrace(); } catch (FileNotFoundException fe) { System.err.println(fe.getMessage()); fe.printStackTrace(); } gen.setST(scanner.getST()); stack = new Stack(); stack = new Stack(); // end of stack stack.push(50); // start state stack.push(0); curState = 0; }
public static void main(String[] args) { try { CLI.parse(args, new String[0]); InputStream inputStream = args.length == 0 ? System.in : new java.io.FileInputStream(CLI.infile); if (CLI.target == Action.SCAN) { CScanner scanner = new CScanner(new DataInputStream(inputStream)); scanner.setTrace(CLI.debug); Token token; boolean done = false; while (!done) { try { for (token = scanner.nextToken(); token.getType() != CParserTokenTypes.EOF; token = scanner.nextToken()) { System.out.println(token.getType()); } done = true; } catch (Exception e) { // print the error: System.out.println("ERROR: " + CLI.infile + " " + e); scanner.consume(); } } } else if (CLI.target == Action.PARSE || CLI.target == Action.DOT || CLI.target == Action.DEFAULT) { CScanner scanner = new CScanner(new DataInputStream(inputStream)); CParser parser = new CParser(scanner); parser.setTrace(CLI.debug); parser.program(); if (parser.getError()) { System.out.println("ERROR"); System.exit(-1); } if (CLI.target == Action.DOT) { System.out.println(TreeVisualizer.generateDOT(parser.getAST())); } } else if (CLI.target == Action.LOWIR) { CScanner scanner = new CScanner(new DataInputStream(inputStream)); CParser parser = new CParser(scanner); parser.setTrace(CLI.debug); parser.program(); if (parser.getError()) { System.out.println("ERROR"); System.exit(-1); } IrProgram cfg = (IrProgram) IrGenerator.getIr(parser.getAST()); SemanticChecker checker = new SemanticChecker(); checker.check(cfg); SymbolTable table = checker.getSymbolTable(); OutputGenerator gen = new OutputGenerator(); gen.generate(cfg, table); } else { System.out.println("Unsupported target"); } } catch (Exception e) { // print the error: System.out.println(CLI.infile + " " + e); } }