Skip to content

Instantly share code, notes, and snippets.

@chenfengyuan
Last active August 29, 2015 13:57
Show Gist options
  • Save chenfengyuan/9703741 to your computer and use it in GitHub Desktop.
Save chenfengyuan/9703741 to your computer and use it in GitHub Desktop.
%lex
%%
\s+ {}
(AND|OR) { return 'LOG'; }
[a-zA-Z]+ { return 'VAR'; }
"(" return '('
")" return ')'
"!" return '!'
<<EOF>> { return 'EOF'; }
/lex
%right '!'
%left 'LOG'
%start expressions
%%
expressions
: exp EOF { return $exp }
;
exp :e|
exp e {$$ = ['AND', $1, $2]};
e:
'!' e {$$ = ['NOT', $2]}|
e 'LOG' e { if($2.match(/^\s+$/)){$$ = ['AND', $1, $3]}else{$$ = [$2.trim(), $1, $3]}}|
var {$$ = $var}|
'(' e ')' { $$ = $2 };
var
: VAR { $$ = yytext; }
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment