internal static Token Synchronize(HashSet<TokenType> types, Scanner scanner, MessageProducer mp) { Token tok = scanner.CurrentToken; // if the current token is not in the synchronization set // then it is unexpected and the parser must recover. if (!types.Contains(tok.TokenType)) { // flag the unexpected token ErrorHandler.Flag(tok, ErrorCode.UNEXPECTED_TOKEN, mp); // recover by skipping tokens that are not in // the synchronization set. do { tok = scanner.GetNextToken(); } while (!tok.IsEof && !types.Contains(tok.TokenType)); } return tok; }
public static AssignmentStatementParser CreateWithObservers(Scanner s, SymbolTableStack stack, List<IMessageObserver> obl) { return NonTerminalParser.CreateWithObserver<AssignmentStatementParser>(s, stack, obl); }
static void Main(string[] args) { bool show_help = false; bool xref = false; bool print_ast = false; string action = "compile"; var names = new List<string>(); var p = new OptionSet() { { "h|help", "show this message and exit", v => show_help = v != null }, { "x|xref", "perform a cross-reference of identifiers", v => xref = v != null }, { "i|intermediate", "print the parse tree of the input", v => print_ast = v != null }, { "a|action=", "`compile' or `interpret' the input", v => action = v }, }; List<string> extra; try { extra = p.Parse(args); } catch (OptionException option_exception) { Console.Write("dradis: "); Console.WriteLine(option_exception.Message); Console.WriteLine("Try `dradis --help' for more information."); return; } if (show_help) { ShowHelp(p); return; } if (extra.Count == 0) { Console.WriteLine("dradis: "); Console.WriteLine("No source file given."); Console.WriteLine("Try `dradis --help' for more information."); return; } string source_path = extra[0]; try { using (StreamReader reader = new StreamReader(source_path)) { SourceMessageObserver sourceObserver = new SourceMessageObserver(); Source source = new Source(reader); source.Add(sourceObserver); Scanner scanner = new Scanner(source); Parser parser = new Parser(scanner); ParserMessageObserver parserObserver = new ParserMessageObserver(); parser.Add(parserObserver); var result = parser.Parse(); if (parser.ErrorCount > 0) { ICode icode = result.Item1; SymbolTableStack symtabstack = result.Item2; if (xref) { XRef.Print(symtabstack); } if (print_ast) { ParseTreePrinter printer = new ParseTreePrinter(Console.Out); printer.Print(icode); } Backend backend = Backend.Create(action); if (backend == null) { throw new Exception("`action' can be either `compile' or 'interpret'!"); } backend.Add(new BackendMessageObserver()); backend.Process(icode, symtabstack); } } } catch(Exception ex) { Console.WriteLine("dradis: "); Console.WriteLine("FATAL ERROR: " + ex.Message); Console.WriteLine("TRACE: " + ex.StackTrace); } }
public static ExpressionParser CreateWithObservers(Scanner s, SymbolTableStack stack, List<IMessageObserver> obl) { return NonTerminalParser.CreateWithObserver<ExpressionParser>(s, stack, obl); }
public Parser(Scanner s) { scanner = s; }