Skip to content
Snippets Groups Projects
Unverified Commit 0e777ca3 authored by Sebastian Höffner's avatar Sebastian Höffner
Browse files

Moving grammar out of the python file.

Additionally, the grammar now uses custom semantics and creates flat
structures.
parent e2be8df7
No related branches found
No related tags found
No related merge requests found
app/openccg_parser.py
# Created by https://www.gitignore.io/api/python
......
app/openccg_parser.py: OpenCCG.ebnf
tatsu --generate-parser $< --outfile $@
@@grammar :: OpenCCG
start
= semspec $
;
semspec
= nominal
| term
| conjunction
| expression
;
conjunction
= @:expression '^' ~ @:conjunction
| @:expression
;
expression
= role_expression
| nominal_expression
| variable_expression
| atom_expression
;
role_expression
= role
;
variable_expression
= variable:variable '^' ~ roles:conjunction
| variable:variable
;
nominal_expression
= nominal:nominal
;
atom_expression
= entity:atom
;
term
= '(' ~ @:conjunction ')'
;
nominal
= '@' ~ nominal:variable roles:term
;
role
= '<' type:atom '>' target:variable
| '<' type:atom '>' target:term
| '<' type:atom '>' target:atom
;
variable
= /[a-z]\d+(:[a-zA-Z\-]+)*/
;
atom
= /[a-zA-Z\-\.]+/
;
from tatsu import parse
from tatsu.ast import AST
from tatsu.util import asjson
GRAMMAR = r"""
start
= semspec $
;
semspec
= nominal
| term
| conjunction
| expression
;
conjunction
= @:expression '^' ~ @:conjunction
| @:expression
;
expression
= role_expression
| nominal_expression
| variable_expression
| atom_expression
;
role_expression
= role:role
;
variable_expression
= variable:variable
;
nominal_expression
= nominal:nominal
;
atom_expression
= atom:atom
;
term
= '(' ~ @:conjunction ')'
;
nominal
= '@' ~ nominal:variable roles:term
;
role
= '<' type:atom '>' target:variable
| '<' type:atom '>' target:term
| '<' type:atom '>' target:atom
;
variable
= /[a-z]\d+(:[a-zA-Z\-]+)*/
;
atom
= /[a-zA-Z\-\.]+/
;
"""
from openccg_parser import OpenCCGParser, OpenCCGSemantics
def ccg_to_json(to_parse):
......@@ -75,4 +12,43 @@ def ccg_to_json(to_parse):
Returns:
A JSON representation of the OpenCCG string.
"""
return asjson(parse(GRAMMAR, to_parse))
return asjson(OpenCCGParser().parse(to_parse,
semantics=OpenCCGCustomSemantics(),
parseinfo=False))
class OpenCCGCustomSemantics(OpenCCGSemantics):
"""The custom semantics are used to flatten the role structure
caused by right-deep trees of the conjunction parses."""
def variable_expression(self, ast):
if ast['roles'] and len(ast['roles']) >= 2:
if isinstance(ast['roles'][-1], list):
role = ast['roles'][-1][0]
del ast['roles'][-1][0]
if len(ast['roles'][-1]) == 0:
del ast['roles'][-1]
ast['roles'].insert(-1, role)
return ast
def role(self, ast):
if isinstance(ast['target'], str):
return ast
if ast['target']['roles'] and len(ast['target']['roles']) >= 2:
for i, maybe_role in enumerate(ast['target']['roles']):
if isinstance(maybe_role, list):
role = ast['target']['roles'][i][0]
del ast['target']['roles'][i][0]
if len(ast['target']['roles'][i]) == 0:
del ast['target']['roles'][i]
ast['target']['roles'].insert(i, role)
if len(ast['target']['roles']) >= 3:
for i, role in enumerate(ast['target']['roles']):
if isinstance(role, list):
r0, r1 = role
del ast['target']['roles'][i]
ast['target']['roles'].insert(i, r1)
ast['target']['roles'].insert(i, r0)
return ast
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment