01: package fri.patterns.interpreter.parsergenerator.syntax.builder.examples;
02:
03: import java.io.*;
04: import fri.patterns.interpreter.parsergenerator.*;
05: import fri.patterns.interpreter.parsergenerator.syntax.Syntax;
06: import fri.patterns.interpreter.parsergenerator.syntax.builder.SyntaxBuilder;
07: import fri.patterns.interpreter.parsergenerator.parsertables.LALRParserTables;
08:
09: /**
10: This example reads the SyntaxBuilder EBNF specification, builds
11: a lexer and parser from it, and parses its own specification with
12: these rules.
13:
14: @author (c) 2002, Fritz Ritzberger
15: */
16:
17: public class Example {
18: public static void main(String[] args) {
19: try {
20: System.out.println("Input is: "
21: + (args.length > 0 ? args[0]
22: : "SyntaxBuilder.syntax"));
23: InputStreamReader in = args.length <= 0 ? new InputStreamReader(
24: Example.class
25: .getResourceAsStream("SyntaxBuilder.syntax"))
26: : new InputStreamReader(
27: new FileInputStream(args[0]));
28: SyntaxBuilder builder = new SyntaxBuilder(in);
29: Syntax syntax = builder.getSyntax();
30: System.out
31: .println("Resolved syntax rules are: ==============");
32: System.out.println(syntax.toString());
33:
34: // now lets see if we can parse the syntax specification text with its own syntax
35: in = args.length <= 0 ? new InputStreamReader(Example.class
36: .getResourceAsStream("SyntaxBuilder.syntax"))
37: : new InputStreamReader(
38: new FileInputStream(args[0]));
39: Lexer lexer = builder.getLexer();
40: lexer.setInput(in);
41: lexer.setDebug(true);
42: Syntax parserSyntax = builder.getParserSyntax();
43: ParserTables parserTables = new LALRParserTables(
44: parserSyntax);
45: Parser parser = new Parser(parserTables);
46: boolean ok = parser.parse(lexer);
47: if (ok == false)
48: throw new Exception(
49: "Failed parsing syntax specification text!");
50: else
51: System.out
52: .println("SyntaxBuilder succeeded parsing its own syntax specification text!");
53: } catch (Exception e) {
54: e.printStackTrace();
55: }
56: }
57:
58: }
|