JavaCC

getNextToken() による読み飛ばし

PARSER_BEGIN(Test) import java.io.*; public class Test { public static void main(String args[]) { Test parser = new Test(System.in); while (true) { try { parser.Input(); } catch (ParseException e) { System.out.println(e.getMessage()); } } …

LOOKAHEAD、規則を記述 再び

任意の規則が書けるのかな? PARSER_BEGIN(Test) import java.io.*; public class Test { public static void main(String args[]) throws ParseException { Test parser = new Test(System.in); parser.Input(); } } PARSER_END(Test) SKIP : { <SPACE: " " | "\t" | "\n" | "\r"> } TOKEN : </space:>…

LOOKAHEAD、文字列と条件式の併用

PARSER_BEGIN(Test) import java.io.*; public class Test { public static void main(String args[]) throws ParseException { Test parser = new Test(System.in); parser.Input(); } } PARSER_END(Test) SKIP : { <SPACE: " " | "\t" | "\n" | "\r"> } TOKEN : { <A: "a"> | <B: "b"> | <C: "c"> | <D: "d"> } void Inpu…</d:></c:></b:></a:></space:>

LOOKAHEAD、トークン列と条件式の併用

PARSER_BEGIN(Test) import java.io.*; public class Test { public static void main(String args[]) throws ParseException { Test parser = new Test(System.in); parser.Input(); } } PARSER_END(Test) SKIP : { <SPACE: " " | "\t" | "\n" | "\r"> } TOKEN : { <A: "a"> | <B: "b"> | <C: "c"> | <D: "d"> } void Inpu…</d:></c:></b:></a:></space:>

LOOKAHEAD、getToken()

PARSER_BEGIN(Test) import java.io.*; public class Test { public static void main(String args[]) throws ParseException { Test parser = new Test(System.in); parser.Input(); } } PARSER_END(Test) SKIP : { <SPACE: " " | "\t" | "\n" | "\r"> } TOKEN : { <A: "a"> | <B: "b"> | <C: "c"> } void Input() : {</c:></b:></a:></space:>…

LOOKAHEAD、条件式を記述

PARSER_BEGIN(Test) import java.io.*; public class Test { public static void main(String args[]) throws ParseException { Test parser = new Test(System.in); parser.Input(); } } PARSER_END(Test) SKIP : { <SPACE: " " | "\t" | "\n" | "\r"> } TOKEN : { <A: "a"> | <B: "b"> | <C: "c"> | <D: "d"> } void Inpu…</d:></c:></b:></a:></space:>

LOOKAHEAD、規則を記述、先読み数を制限

PARSER_BEGIN(Test) import java.io.*; public class Test { public static void main(String args[]) throws ParseException { Test parser = new Test(System.in); parser.Input(); } } PARSER_END(Test) SKIP : { <SPACE: " " | "\t" | "\n" | "\r"> } TOKEN : { <A: "a"> | <B: "b"> | <C: "c"> | <D: "d"> } void Inpu…</d:></c:></b:></a:></space:>

LOOKAHEAD、文字列列を記述

PARSER_BEGIN(Test) import java.io.*; public class Test { public static void main(String args[]) throws ParseException { Test parser = new Test(System.in); parser.Input(); } } PARSER_END(Test) SKIP : { <SPACE: " " | "\t" | "\n" | "\r"> } void Input() : {} { ( LOOKAHEAD("a"</space:>…

LOOKAHEAD、トークン列を記述

PARSER_BEGIN(Test) import java.io.*; public class Test { public static void main(String args[]) throws ParseException { Test parser = new Test(System.in); parser.Input(); } } PARSER_END(Test) SKIP : { <SPACE: " " | "\t" | "\n" | "\r"> } TOKEN : { <A: "a"> | <B: "b"> | <C: "c"> | <D: "d"> } void Inpu…</d:></c:></b:></a:></space:>

LOOKAHEAD、規則を記述

PARSER_BEGIN(Test) import java.io.*; public class Test { public static void main(String args[]) throws ParseException { Test parser = new Test(System.in); parser.Input(); } } PARSER_END(Test) SKIP : { <SPACE: " " | "\t" | "\n" | "\r"> } TOKEN : { <A: "a"> | <B: "b"> | <C: "c"> | <D: "d"> } void Inpu…</d:></c:></b:></a:></space:>

LOOKAHEAD、変な書き方をしてみる

PARSER_BEGIN(Test) import java.io.*; public class Test { public static void main(String args[]) throws ParseException { Test parser = new Test(System.in); parser.Input(); } } PARSER_END(Test) SKIP : { <SPACE: " " | "\t" | "\n" | "\r"> } TOKEN : { <A: "a"> | <B: "b"> | <C: "c"> | <D: "d"> } void Inpu…</d:></c:></b:></a:></space:>

LOOKAHEAD、構文規則中に記述

複数 LOOKAHEAD を書いたときの動作再確認 PARSER_BEGIN(Test) import java.io.*; public class Test { public static void main(String args[]) throws ParseException { Test parser = new Test(System.in); parser.Input(); } } PARSER_END(Test) SKIP : …

LOOKAHEAD、構文規則中に記述

二つ書くと? PARSER_BEGIN(Test) import java.io.*; public class Test { public static void main(String args[]) throws ParseException { Test parser = new Test(System.in); parser.Input(); } } PARSER_END(Test) SKIP : { <SPACE: " " | "\t" | "\n" | "\r"> } TOKEN : { <A: "a"> | <B: "b"> | <C: "c"> | <D: "d"> | </d:></c:></b:></a:></space:>

LOOKAHEAD、構文規則中に記述

PARSER_BEGIN(Test) import java.io.*; public class Test { public static void main(String args[]) throws ParseException { Test parser = new Test(System.in); parser.Input(); } } PARSER_END(Test) SKIP : { <SPACE: " " | "\t" | "\n" | "\r"> } TOKEN : { <A: "a"> | <B: "b"> | <C: "c"> | <D: "d"> } void Inpu…</d:></c:></b:></a:></space:>

左くくり出し

PARSER_BEGIN(Test) import java.io.*; public class Test { public static void main(String args[]) throws ParseException { Test parser = new Test(System.in); parser.Input(); } } PARSER_END(Test) SKIP : { <SPACE: " " | "\t" | "\n" | "\r"> } TOKEN : { <A: "a"> | <B: "b"> | <C: "c"> | <D: "d"> } void Inpu…</d:></c:></b:></a:></space:>

左再帰

PARSER_BEGIN(Test) import java.io.*; public class Test { public static void main(String args[]) throws ParseException { Test parser = new Test(System.in); parser.Input(); } } PARSER_END(Test) SKIP : { <SPACE: " " | "\t" | "\n" | "\r"> } TOKEN : { <A: "a"> } void Input() : {} { In</a:></space:>…

エラー処理

特に何もせずにシンプルに PARSER_BEGIN(Test) import java.io.*; public class Test { public static void main(String args[]) { Test parser = new Test(System.in); while (true) { try { parser.Input(); } catch (ParseException e) { System.out.prin…

エラー処理

1行ごとに処理するとした場合毎行 parser を生成 options { STATIC = false; } PARSER_BEGIN(Test) import java.io.*; public class Test { public static void main(String args[]) { String line = null; try { BufferedReader inStream = new BufferedRea…

入力形式

PARSER_BEGIN(Test) public class Test { public static void main(String args[]) throws ParseException { Test parser = new Test(System.in); parser.Input(); } } PARSER_END(Test) SKIP : { <SPACE: " " | "\t" | "\n" | "\r"> } TOKEN : { <A: "a"> | <B: "b"> } void Input() : {} { ( <A> )+ <EOF> } で、 a a</eof></a></b:></a:></space:>…

.jj 内に main を書かない

PARSER_BEGIN(Test) public class Test { } PARSER_END(Test) SKIP : { <SPACE: " " | "\t" | "\n" | "\r"> } TOKEN : { <A: "a"> | <B: "b"> } void Input() : { Token t; } { ( (t = <A> | t = <B> | t = "c" | t = "def") { System.out.println("*" + t.image + "*"); } )+ <EOF> } と、 public class TestMain { public</eof></b></a></b:></a:></space:>…

自前の Token リーダー

options { USER_TOKEN_MANAGER = true; } PARSER_BEGIN(Test) public class Test { public static void main(String args[]) throws ParseException { Test parser = new Test(new TestMyTokenManager()); parser.Input(); } } PARSER_END(Test) void Input(…

トークンのデバッグコード

『JavaCC―コンパイラ・コンパイラfor Java』の simple/sent3/sent3tdbg.java を参考にして、 昨日はちょっと勘違いして .jj に埋め込んでしまったので、独立した .java として動作させ、 a TOKEN="a" image="a" c TOKEN="c" image="c" def TOKEN="def" image…

トークンのデバッグコード

『JavaCC―コンパイラ・コンパイラfor Java』の simple/sent3/sent3tdbg.java を参考にして、 a TOKEN="a" image="a" c TOKEN="c" image="c" def TOKEN="def" image="def" TOKEN=<EOF> image=""</eof>

DEBUG_TOKEN_MANAGER

options { DEBUG_TOKEN_MANAGER = true; } PARSER_BEGIN(Test) public class Test { public static void main(String args[]) throws ParseException { Test parser = new Test(System.in); parser.Input(); } } PARSER_END(Test) SKIP : { <SPACE: " " | "\t" | "\n" | "\r"> } TOKEN : { <A: "a"> | <B: "b"></b:></a:></space:>…

*Constants.java

PARSER_BEGIN(Test) public class Test { public static void main(String args[]) throws ParseException { Test parser = new Test(System.in); parser.Input(); } } PARSER_END(Test) SKIP : { <SPACE: " " | "\t" | "\n" | "\r"> } TOKEN : { <A: "a"> | <B: "b"> } void Input() : {} { ( <A> | <B> | "c" | "de</b></a></b:></a:></space:>…

getNextToken()

字句解析内での呼び出し PARSER_BEGIN(Test) public class Test { public static void main(String args[]) throws ParseException { Test parser = new Test(System.in); parser.Input(); } } PARSER_END(Test) SKIP : { <SPACE: " " | "\t" | "\n" | "\r"> } TOKEN : { <A: "a"> { getNextToken(); }</a:></space:>…

Token の位置情報、next

matchedToken も Token クラスだから、それと同じか〜 PARSER_BEGIN(Test) public class Test { public static void main(String args[]) throws ParseException { Test parser = new Test(System.in); parser.Input(); } } PARSER_END(Test) SKIP : { <SPACE: " " | "\t" | "\n" | "\r"> } TO</space:>…

字句解析:input_stream.get*()

SKIP 内では、matchedToken が使えないから、input_stream.get*() を使うらしい(使う機会は余りなさそうな気もするけど) PARSER_BEGIN(Test) public class Test { public static void main(String args[]) throws ParseException { Test parser = new Test(S…

字句解析:SwitchTo()

PARSER_BEGIN(Test) public class Test { public static void main(String args[]) throws ParseException { Test parser = new Test(System.in); parser.Input(); } } PARSER_END(Test) <*>SKIP : { <SPACE: " " | "\t" | "\n" | "\r"> } TOKEN : { <A: "a"> : STATE1 } <STATE1>TOKEN : { <S1A: "a"> { SwitchTo(0); } }</s1a:></state1></a:></space:>…

字句解析:input_stream

PARSER_BEGIN(Test) public class Test { public static void main(String args[]) throws ParseException { Test parser = new Test(System.in); parser.Input(); } } PARSER_END(Test) SKIP : { <SPACE: " " | "\t" | "\n" | "\r"> } TOKEN : { <A: "a"> { System.out.print(input_stream); } } void</a:></space:>…