Skip to content

Commit

Permalink
[HA-11.0] Add initial sollution for 11th task
Browse files Browse the repository at this point in the history
  • Loading branch information
IThror10 committed May 29, 2024
1 parent a204094 commit 00aeeaa
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ jobs:
run: |
python -m pip install --upgrade pip
python -m pip install -r ./requirements.txt
- name: Generate parser
run: antlr4 -Dlanguage=Python3 project/langRules.yaml -visitor -o project/lang
- name: Test with pytest
run: |
python ./scripts/run_tests.py
21 changes: 21 additions & 0 deletions project/langRules.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
grammar langRules;

prog: stmt*;

stmt: bind | add | remove | declare;
declare: 'let' VAR 'is' 'graph';
bind: 'let' VAR '=' expr;
remove: 'remove' ('vertex' | 'edge' | 'vertices') expr 'from' VAR;
add: 'add' ('vertex' | 'edge') expr 'to' VAR;

expr: NUM | CHAR | VAR | edge_expr | set_expr | regexp | select;
set_expr: '[' expr (',' expr)* ']';
edge_expr: '(' expr ',' expr ',' expr ')';
regexp: CHAR | VAR | '(' regexp ')' | regexp '|' regexp | regexp '^' range | regexp '.' regexp | regexp '&' regexp;
range: '[' NUM '..' NUM? ']';
select: v_filter? v_filter? 'return' VAR (',' VAR)? 'where' VAR 'reachable' 'from' VAR 'in' VAR 'by' expr;
v_filter: 'for' VAR 'in' expr;

VAR: [a-z] [a-z0-9]*;
NUM: ([1-9][0-9]*) | '0';
CHAR: '\u0022' [a-z] '\u0022';
41 changes: 41 additions & 0 deletions project/task11.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from project.lang.project.languageVisitor import languageVisitor
from project.lang.project.languageLexer import languageLexer
from project.lang.project.languageParser import languageParser

from antlr4 import *
from antlr4.InputStream import InputStream


class NodeCounter(languageVisitor):
def __init__(self):
super().__init__()
self.counter = 0

def enterEveryRule(self, _):
self.counter += 1


class TreeToProgVisitor(languageVisitor):
def __init__(self):
super().__init__()
self.visits = []

def enterEveryRule(self, rule):
self.visits.append(rule.get_text())


def nodes_count(tree: ParserRuleContext) -> int:
visitor = NodeCounter()
tree.accept(visitor)
return visitor.counter


def tree_to_prog(tree: ParserRuleContext) -> str:
visitor = TreeToProgVisitor()
tree.accept(visitor)
return "".join(visitor.visits)


def prog_to_tree(program: str) -> tuple[ParserRuleContext, bool]:
parser = languageParser(CommonTokenStream(languageLexer(InputStream(program))))
return parser.prog(), parser.getNumberOfSyntaxErrors() == 0
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
antlr4-python3-runtime
antlr4-tools
black
cfpq-data
grammarinator @ git+https://github.com/renatahodovan/grammarinator.git@f3ffa71
Expand Down

0 comments on commit 00aeeaa

Please sign in to comment.