Javacc

xiaoxiao2021-03-05  27

Javacc

Java version Yacc Lex, Lucene uses this to do query analysis. Official website https://javacc.dev.java.net/

IBM DW introduces javacc, parsing tree and xquery syntax http://www-900.ibm.com/developerWorks/cn/xml/x-javacc/part1/index.shtml Now you can define your own language! http://www-900.ibm.com/developerWorks/cn/dmdd/library/techarticles/dm-0401brereton/index.shtml

JavaWorld About the article Build Your Own Languages ​​with javacc: http://www.javaworld.com/javaworld/jw 12-2000/jw-1229-cooltools-p2.html

JAVACC uses .jj file

A simple calculator Calculator.jj generates Parserjjdoc Calculator.jj Gene BNF using javacc calculator.jj

Options

{

LOOKAHEAD = 2;

}

Parser_Begin (Calculator)

Public Class Calculator

{

Public static void main (string args []) Throws Parsexception

{

Calculator Parser = New Calculator (System.in);

While (True)

{

Parser.ParseOnline ();

}

}

}

Parser_end (Calculator)

SKIP:

{

""

| "/ r"

| "/ t"

}

TOKEN:

{

) ("."

) )?>

|

|

}

Void ParseOnline ():

{

Double A;

}

{

A = expr ()

{System.out.println (a);

|

|

{System.exit (-1);

}

Double EXPR ():

{

Double A;

Double B;

}

{

a = term ()

(

" " b = expr () {a = b;}

| "-" b = expr () {a - = b;}

) *

{RETURN A;}

}

Double Term ():

{

Double A;

Double B;

}

{

a = unary ()

(

"*" b = term () {a * = b;}

| "/" b = term () {a / = b;}

) *

{RETURN A;}

}

Double unary ():

{

Double A;

}

{

"-" A = Element () {return -a;}

| a = Element () {Return A;}

}

Double Element ():

{

Token T;

Double A;

}

{

t =

{Return Double.Parsedouble (t.toString ());

| "(" A = EXPR () ")" {Return A;}}

From Xinyun: JAVACC

转载请注明原文地址:https://www.9cbs.com/read-34465.html

New Post(0)