add maxLength argument to Input.showRegion()
[sbp.git] / src / edu / berkeley / sbp / misc / Demo2.java
1 // Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
2
3 package edu.berkeley.sbp.misc;
4
5 import edu.berkeley.sbp.*;
6
7 public class Demo2 {
8
9     private static Atom atom(char c) {
10         return new edu.berkeley.sbp.chr.CharAtom(c); }
11     private static Atom atom(char c1, char c2) {
12         return new edu.berkeley.sbp.chr.CharAtom(c1, c2); }
13
14     public static void main(String[] s) throws Exception {
15
16         Union expr = new Union("Expr");
17
18         Element[] add   = new Element[] { expr, atom('+'), expr };
19         Element[] mult  = new Element[] { expr, atom('*'), expr };
20         Element[] paren = new Element[] { atom('('), expr, atom(')') };
21         
22         Sequence addSequence = Sequence.create("add", add, null, false);
23         Sequence multSequence = Sequence.create("mult", mult, null, false);
24
25         // uncomment this line to disambiguate
26         //multSequence = multSequence.andnot(Sequence.create("add", add, null, false));
27
28         expr.add(Sequence.create(paren, 1));
29         expr.add(addSequence);
30         expr.add(multSequence);
31         expr.add(Sequence.create(atom('0', '9')));
32
33         edu.berkeley.sbp.chr.CharInput input = new edu.berkeley.sbp.chr.CharInput("(1+3*8)*7");
34
35         System.out.println("input:  \""+input+"\"");
36
37         StringBuffer sb = new StringBuffer();
38         expr.toString(sb);
39         System.out.println("grammar: \n"+sb);
40
41         Forest f = new edu.berkeley.sbp.chr.CharParser(expr).parse(input);
42         try {
43             System.out.println("output: "+f.expand1().toPrettyString());
44         } catch (Ambiguous a) {
45             System.err.println(a.toString());
46             System.err.println(" ambiguous text: " + input.showRegion(a.getRegion()));
47         }
48     }
49
50 }