Skip to content

Instantly share code, notes, and snippets.

@leyyce
Created July 10, 2022 18:43
Show Gist options
  • Save leyyce/c28da283604f779f9c2505605cb6c31b to your computer and use it in GitHub Desktop.
Save leyyce/c28da283604f779f9c2505605cb6c31b to your computer and use it in GitHub Desktop.
options {
LOOKAHEAD = 1;
CHOICE_AMBIGUITY_CHECK = 2;
OTHER_AMBIGUITY_CHECK = 1;
STATIC = true;
DEBUG_PARSER = false;
DEBUG_LOOKAHEAD = false;
DEBUG_TOKEN_MANAGER = false;
ERROR_REPORTING = true;
JAVA_UNICODE_ESCAPE = false;
UNICODE_INPUT = false;
IGNORE_CASE = false;
USER_TOKEN_MANAGER = false;
USER_CHAR_STREAM = false;
BUILD_PARSER = true;
BUILD_TOKEN_MANAGER = true;
SANITY_CHECK = true;
FORCE_LA_CHECK = false;
OUTPUT_DIRECTORY = "src/io/elcapitan/parserGen";
}
PARSER_BEGIN(ExampleParser)
package genParser;
public class ExampleParser {
public static void main(String[] args) throws ParseException {
ExampleParser parser = new ExampleParser(System.in);
boolean solution = parser.exp();
System.out.println(solution);
}
}
PARSER_END(ExampleParser)
boolean exp():
{
char cmpOp;
char op;
int comp;
int numLeft;
int numRight;
boolean bool;
boolean fi;
boolean fiReturn;
boolean elseReturn;
}
{
"i" fi=exp() "t" fiReturn=exp() "e" elseReturn=exp() "f"
{
if (fi) return fiReturn;
else return elseReturn;
}
| "2" cmpOp=cmp() comp=aexp()
{
if (cmpOp == '>') return 2 > comp;
else if(cmpOp == '<') return 2 < comp;
else return 2 == comp;
}
| "3" cmpOp=cmp() comp=aexp()
{
if (cmpOp == '>') return 3 > comp;
else if(cmpOp == '<') return 3 < comp;
else return 3 == comp;
}
| "4" cmpOp=cmp() comp=aexp()
{
if (cmpOp == '>') return 4 > comp;
else if(cmpOp == '<') return 4 < comp;
else return 4 == comp;
}
| ":" numLeft=aexp() op=op() numRight=aexp() ";" cmpOp=cmp() comp=aexp()
{
int solution;
if (op == '+') solution = numLeft + numRight;
else solution = numLeft - numRight;
if (cmpOp == '>') return solution > comp;
else if(cmpOp == '<') return solution < comp;
else return solution == comp;
}
| "n" bool=exp() {return !bool;}
| "0" {return false;}
| "1" {return true;}
}
int aexp() :
{
int numLeft;
int numRight;
char op;
}
{
"2" {return 2;} | "3" {return 3;} | "4" {return 4;}
| ":" numLeft=aexp() op=op() numRight=aexp() ";"
{
if(op == '+') return numLeft + numRight;
else return numLeft - numRight;
}
}
char cmp() :
{}
{
"<" {return '<';} | ">" {return '>';} | "=" {return '=';}
}
char op() :
{}
{
"+" {return '+';} | "-" {return '-';}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment