📄 oaaprolognetparse.g
字号:
header {
package com.sri.oaa2.icl;
import java.util.Stack;
}
class OaaPrologParser extends Parser;
options {
k = 2;
exportVocab = OaaPrologVocab;
buildAST = true;
defaultErrorHandler=false;
}
tokens {
GROUP;
LIST;
VAR;
STRUCT;
ICLDATAQ;
STR;
INT;
FLOAT;
DOT;
TERM_LITERAL="term";
ENDPAREN_LITERAL=").";
ICLDATAQ_LITERAL="icldataq";
// JIS7START_LITERAL="\u0027$B";
// JIS7END_LITERAL="\u0027(B";
}
{
private Stack listStack = new Stack();
}
startMulti
: (DOT!)? netstruct
;
//startMulti
// : (DOT!)? struct
// ;
startOneOnly
: (struct (DOT!)? | )
EOF!
;
startMultiNonNet
: struct (DOT!)?
;
icldataqTest
: (icldataqStruct | )
EOF!
;
//expression
// : group
// | list
// | struct
// ;
commaSeparatedStructs
: struct
(COMMA!
struct
)*
;
listOnlyExpressionList
: ( struct
(COMMA!
struct
)*
(PIPE!
( var {((IclASTList)this.listStack.peek()).setHeadTailList(true);}
| recursiveList! {AST child = returnAST; AST nextChild = null; while(child != null){nextChild = child.getNextSibling();astFactory.addASTChild(currentAST, child); child = nextChild;}})
)?
|
)
;
group
: LBRACE^<AST=IclASTGroup>
commaSeparatedStructs
RBRACE! {currentAST.root.setType(GROUP);}
| LPAREN^<AST=IclASTGroup>
commaSeparatedStructs
RPAREN! {currentAST.root.setType(GROUP);}
;
list
:
LBRACK^<AST=IclASTList>{this.listStack.push(currentAST.root);}
listOnlyExpressionList
RBRACK!
{this.listStack.pop();currentAST.root.setType(LIST);}
;
recursiveList
:
LBRACK!
listOnlyExpressionList
RBRACK!
;
var
: VARIABLE<AST=IclASTVar>
{currentAST.root.setType(VAR);}
;
netstruct
: TERM_LITERAL^<AST=IclASTStruct>
LPAREN!
commaSeparatedStructs
RPAREN!
{currentAST.root.setType(STRUCT);}
;
// * / (2)
// + - :- : ; (3)
struct
:
semiExpression ((TURNSTILE^<AST=IclASTStruct>) semiExpression)*
;
semiExpression
:
backslashExpression ((SEMI^<AST=IclASTStruct>) backslashExpression)*
;
backslashExpression
:
equalsExpression ((BACKSLASH^<AST=IclASTStruct>) equalsExpression)*
;
equalsExpression
:
colonExpression ((EQUAL^<AST=IclASTStruct>) colonExpression)*
;
colonExpression
:
plusMinusExpression (( COLON^<AST=IclASTStruct> | DBL_COLON^<AST=IclASTStruct>) plusMinusExpression)*
;
plusMinusExpression
:
multiplicativeExpression (( PLUS^<AST=IclASTStruct> | MINUS^<AST=IclASTStruct> ) multiplicativeExpression)*
;
multiplicativeExpression
:
unaryExpression (( STAR^<AST=IclASTStruct> | DIV^<AST=IclASTStruct> ) unaryExpression)*
;
// Warning of nondeterminism between (1,3) and (2,3) is okay since we'll just take
// PLUS^ and MINUS^ by default, instead of the atom called PLUS or MINUS (unless, of
// course, there is no unaryExpression occuring after the PLUS or MINUS)
unaryExpression
{int sign = 1;}
: normalStruct
| icldataqStruct
| PLUS! NUM_INT^<AST=IclASTInt> {currentAST.root.setType(INT);}
| PLUS! NUM_FLOAT^<AST=IclASTFloat> {currentAST.root.setType(FLOAT);}
| MINUS! {sign = -1;} NUM_INT^<AST=IclASTInt> {currentAST.root.setType(INT); ((IclASTInt)(currentAST.root)).setSign(sign);}
| MINUS! {sign = -1;} NUM_FLOAT^<AST=IclASTFloat> {currentAST.root.setType(FLOAT);((IclASTFloat)(currentAST.root)).setSign(sign);}
| PLUS^<AST=IclASTStruct> unaryExpression {currentAST.root.setType(STRUCT);}
| MINUS^<AST=IclASTStruct> unaryExpression {currentAST.root.setType(STRUCT);}
| unaryExpressionNotPlusMinus
;
unaryExpressionNotPlusMinus
: list
| group
| var
| NUM_INT<AST=IclASTInt> {currentAST.root.setType(INT);}
| NUM_FLOAT<AST=IclASTFloat> {currentAST.root.setType(FLOAT);}
| str
;
icldataqStruct
: ICLDATAQ_LITERAL!
LPAREN!
(icldataqShortStruct | icldataqLongStruct)
RPAREN!
;
icldataqShortStruct
: DBLQUOTED^<AST=IclASTDataQ>
{currentAST.root.setType(ICLDATAQ);}
;
icldataqLongStruct
: rawlen:NUM_INT! COMMA! numquotes:NUM_INT! COMMA! DBLQUOTED^<AST=IclASTDataQ>
{currentAST.root.setType(ICLDATAQ); ((IclASTDataQ)(currentAST.root)).setRawLen(rawlen); ((IclASTDataQ)(currentAST.root)).setNumQuotes(numquotes);}
;
normalStruct
: (BANG^<AST=IclASTStruct> | SEMI^<AST=IclASTStruct> | STRING_LITERAL^<AST=IclASTStruct> | SPECIAL_CHAR_LITERAL^<AST=IclASTStruct> | IDENT^<AST=IclASTStruct> | PLUS^<AST=IclASTStruct> | MINUS^<AST=IclASTStruct> | STAR^<AST=IclASTStruct> | DIV^<AST=IclASTStruct> | EQUAL^<AST=IclASTStruct>| TURNSTILE^<AST=IclASTStruct> | COLON^<AST=IclASTStruct> | DBL_COLON^<AST=IclASTStruct> | BACKSLASH^<AST=IclASTStruct>)
LPAREN!
commaSeparatedStructs
RPAREN!
{currentAST.root.setType(STRUCT);}
;
str
: BANG<AST=IclASTStr>
| SEMI<AST=IclASTStr>
| STRING_LITERAL<AST=IclASTStr>
| SPECIAL_CHAR_LITERAL<AST=IclASTStr>
| STAR<AST=IclASTStr>
| PLUS<AST=IclASTStr>
| MINUS<AST=IclASTStr>
| DIV<AST=IclASTStr>
| EQUAL<AST=IclASTStr>
| TURNSTILE<AST=IclASTStr>
| IDENT<AST=IclASTStr>
| TERM_LITERAL<AST=IclASTStr>
| ICLDATAQ_LITERAL<AST=IclASTStr>
{currentAST.root.setType(STR);}
;
class OaaPrologLexer extends Lexer;
options {
k = 2;
exportVocab = OaaPrologVocab;
testLiterals = true;
charVocabulary = '\u0000'..'\u00FF';
}
{
private boolean throwOnEOF = false;
public final void setThrowOnEOF(boolean t)
{
throwOnEOF = t;
}
public final boolean getThrowOnEOF()
{
return throwOnEOF;
}
public final void uponEOF() throws TokenStreamException, CharStreamException {
if(getThrowOnEOF()) {
throw new TokenStreamEOF("EOF");
}
}
}
// Operators
LPAREN options { paraphrase = "("; } : '(';
RPAREN options { paraphrase = ")"; } : ')';
LBRACE options { paraphrase = "{"; } : '{';
RBRACE options { paraphrase = "}"; } : '}';
LBRACK options { paraphrase = "["; } : '[';
RBRACK options { paraphrase = "]"; } : ']';
//STAR options { paraphrase = "*"; } : '*';
//DIV options { paraphrase = "/"; } : '/';
//PLUS options { paraphrase = "+"; } : '+';
//MINUS options { paraphrase = "-"; } : '-';
//TURNSTILE: ":-";
//COLON options { paraphrase = ":"; } : ':';
SEMI options { paraphrase = ";"; } : ';';
BANG options { paraphrase = "!"; } : '!';
COMMA options { paraphrase = ","; } : ',';
DBLQUOTE options { paraphrase = "\""; } : '"';
TILDE options { paraphrase = "~";} : '~';
PIPE options { paraphrase = "|";} : '|';
//BACKSLASH options { paraphrase = "\\"} : '\\';
//EQUAL options { paraphrase = "=";} : '=';
//DOT options { paraphrase = "."; } : '.';
// Whitespace
WS : ( ' '
| '\t'
| '\f'
// newlines
| ( "\r\n"
| '\r'
| '\n'
)
{ newline(); }
)
{ _ttype = Token.SKIP; }
;
//ENDPAREN options {paraphrase = ")";} : ')' {_ttype = RPAREN;} ('.' {_ttype = ENDPAREN;})?
// ;
STRING_LITERAL options {paraphrase = "string literal";}
:'\'' ( "''" | ~'\'' )* '\''
;
DBLQUOTED options {paraphrase = "double quoted data";}
:DBLQUOTE (( "\"\"" | ~'"' )*) DBLQUOTE
;
//JIS7START options {paraphrase = "JIS7 start token";} : '\u0027' '$' 'B' ;
//JIS7END options {paraphrase = "JIS7 end token";} : '\u0027' '(' 'B' ;
SPECIAL_CHAR_LITERAL options {paraphrase = "special character literal";}
{boolean isTurnstile=false; boolean isDblColon=false; boolean isPlus=false; boolean isMinus=false; boolean isStar=false; boolean isDiv=false; boolean isEqual=false; boolean isColon=false; boolean isBackslash=false;}
: ("+" {_ttype = PLUS; isPlus=true;})
{isPlus}?
(('+' | '-' | '*' | '^' | '<' | '>' | '=' | '`' | ':' | '.' | '?' | '@' | '#' | '$' | '&' | '/' | '\\')+ {isPlus=false; _ttype = SPECIAL_CHAR_LITERAL;})?
| ("-" {_ttype = MINUS; isMinus=true;})
{isMinus}?
(('+' | '-' | '*' | '^' | '<' | '>' | '=' | '`' | ':' | '.' | '?' | '@' | '#' | '$' | '&' | '/' | '\\')+ {isMinus=false; _ttype = SPECIAL_CHAR_LITERAL;})?
| ("*" {_ttype = STAR; isStar=true;})
{isStar}?
(('+' | '-' | '*' | '^' | '<' | '>' | '=' | '`' | ':' | '.' | '?' | '@' | '#' | '$' | '&' | '/' | '\\')+ {isStar=false; _ttype = SPECIAL_CHAR_LITERAL;})?
| ("/" {_ttype = DIV; isDiv=true;})
{isDiv}?
(('+' | '-' | '*' | '^' | '<' | '>' | '=' | '`' | ':' | '.' | '?' | '@' | '#' | '$' | '&' | '/' | '\\')+ {isDiv=false; _ttype = SPECIAL_CHAR_LITERAL;})?
| ("=" {_ttype = EQUAL; isEqual=true;})
{isEqual}?
(('+' | '-' | '*' | '^' | '<' | '>' | '=' | '`' | ':' | '.' | '?' | '@' | '#' | '$' | '&' | '/' | '\\')+ {isEqual=false; _ttype = SPECIAL_CHAR_LITERAL;})?
| ("\\" {_ttype = BACKSLASH; isBackslash=true;})
{isBackslash}?
(('+' | '-' | '*' | '^' | '<' | '>' | '=' | '`' | ':' | '.' | '?' | '@' | '#' | '$' | '&' | '/' | '\\')+ {isBackslash=false; _ttype = SPECIAL_CHAR_LITERAL;})?
// : PLUS ('+' | '-' | '*' | '^' | '<' | '>' | '=' | '`' | ':' | '.' | '?' | '@' | '#' | '$' | '&' | '/' | '\\')+
// | MINUS ( '+' | '-' | '*' | '^' | '<' | '>' | '=' | '`' | ':' | '.' | '?' | '@' | '#' | '$' | '&' | '/' | '\\')+
// | STAR ( '+' | '-' | '*' | '^' | '<' | '>' | '=' | '`' | ':' | '.' | '?' | '@' | '#' | '$' | '&' | '/' | '\\')+
// | DIV ( '+' | '-' | '*' | '^' | '<' | '>' | '=' | '`' | ':' | '.' | '?' | '@' | '#' | '$' | '&' | '/' | '\\')+
| (':' {_ttype = COLON; isColon=true;})
{isColon}?
(
('-' {_ttype = TURNSTILE; isColon=false; isTurnstile=true;})
{isTurnstile}?
(('+' | '-' | '*' | '^' | '<' | '>' | '=' | '`' | ':' | '.' | '?' | '@' | '#' | '$' | '&' | '/' | '\\')+ {isTurnstile=false; _ttype = SPECIAL_CHAR_LITERAL;})?
|
(':' {_ttype = DBL_COLON; isColon=false; isDblColon=true;})
{isDblColon}?
(('+' | '-' | '*' | '^' | '<' | '>' | '=' | '`' | ':' | '.' | '?' | '@' | '#' | '$' | '&' | '/' | '\\')+ {isDblColon=false; _ttype = SPECIAL_CHAR_LITERAL;})?
)?
// | (":-" {_ttype = TURNSTILE; isTurnstile=true;})
// {isTurnstile}?
// (('+' | '-' | '*' | '^' | '<' | '>' | '=' | '`' | ':' | '.' | '?' | '@' | '#' | '$' | '&' | '/' | '\\')+ {isTurnstile=false; _ttype = SPECIAL_CHAR_LITERAL;})?
// | ("::" {_ttype = DBL_COLON; isDblColon=true;})
// {isDblColon}?
// (('+' | '-' | '*' | '^' | '<' | '>' | '=' | '`' | ':' | '.' | '?' | '@' | '#' | '$' | '&' | '/' | '\\')+ {isDblColon=false; _ttype = SPECIAL_CHAR_LITERAL;})?
| ('^' | '<' | '>' | '`' | ':' | '?' | '@' | '#' | '$' | '&' | '\\') ( '+' | '*' | '=' | '^' | '<' | '>' | '=' | '`' | '.' | '?' | '@' | '#' | '$' | '&' | '/' | '\\') ( '+' | '-' | '*' | '^' | '<' | '>' | '=' | '`' | ':' | '.' | '?' | '@' | '#' | '$' | '&' | '/' | '\\')*
| ('^' | '<' | '>' | '`' | ':' | '?' | '@' | '#' | '$' | '&' | '\\')+
;
NUM_INT options {paraphrase = "number";}
{boolean isDecimal=false; boolean isFloat = false;}
: '.' {_ttype = DOT;}
( (('0'..'9')+ (EXPONENT)? { _ttype = NUM_FLOAT;})
| (( '+' | '-' | '*' | '^' | '<' | '>' | '=' | '`' | ':' | '.' | '?' | '@' | '#' | '$' | '&' | '/' | '\\')+ {_ttype = SPECIAL_CHAR_LITERAL;})
)?
| ('0'..'9') ('0'..'9')* {isDecimal = true;}
{isDecimal}?
( ('.' ('0'..'9')* (EXPONENT)?) { _ttype = NUM_FLOAT;}
| SIGNED_EXPONENT { _ttype = NUM_FLOAT;}
| ((('E' | 'e') ('0'..'9')+) {_ttype = NUM_FLOAT; isFloat = true;}
{isFloat}?
(('a'..'z'|'A'..'Z'|'_'|'\u00df'..'\u00f6'|'\u00f8'..'\u00ff'|'\u00c0'..'\u00d6'|'\u00d8'..'\u00de') ('a'..'z'|'A'..'Z'|'_'|'0'..'9'|'\u00df'..'\u00f6'|'\u00f8'..'\u00ff'|'\u00c0'..'\u00d6'|'\u00d8'..'\u00de')* {_ttype = IDENT;})?
)
)?
;
IDENT options {paraphrase = "atom";}
: ('a'..'z'|'\u00df'..'\u00f6'|'\u00f8'..'\u00ff') ('a'..'z'|'A'..'Z'|'_'|'0'..'9'|'\u00df'..'\u00f6'|'\u00f8'..'\u00ff'|'\u00c0'..'\u00d6'|'\u00d8'..'\u00de')*
;
VARIABLE options {paraphrase = "variable";}
: ('A'..'D' | 'F'..'Z' | '_'|'\u00c0'..'\u00d6'|'\u00d8'..'\u00de') ('a'..'z'|'A'..'Z'|'_'|'0'..'9'|'\u00df'..'\u00f6'|'\u00f8'..'\u00ff'|'\u00c0'..'\u00d6'|'\u00d8'..'\u00de')*
| 'E' ('0'..'9')* ('a'..'z'|'A'..'Z'|'_'|'\u00df'..'\u00f6'|'\u00f8'..'\u00ff'|'\u00c0'..'\u00d6'|'\u00d8'..'\u00de') ('a'..'z'|'A'..'Z'|'_'|'0'..'9'|'\u00df'..'\u00f6'|'\u00f8'..'\u00ff'|'\u00c0'..'\u00d6'|'\u00d8'..'\u00de')*
| 'E'
;
protected
EXPONENT options {paraphrase = "exponent";}
: ('E'|'e') ('+'|'-')? ('0'..'9')+
;
protected
SIGNED_EXPONENT options {paraphrase = "signed exponent";}
: ('E'|'e') ('+'|'-') ('0'..'9')+
;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -