Skip to content

Commit

Permalink
Merge pull request #14 from TreshMom/task-11
Browse files Browse the repository at this point in the history
Перелив для 12 таски
  • Loading branch information
TreshMom authored Jun 25, 2024
2 parents 93a2e91 + 476bfcd commit 2de9053
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 36 deletions.
Binary file modified .DS_Store
Binary file not shown.
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ jobs:
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Generate antlr4 dependencies
run: antlr4 -Dlanguage=Python3 -Xexact-output-dir project/lang.g4 -o project/lang

- name: Run tests
run: |
python scripts/run_tests.py
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ celerybeat.pid
# Environments
.env
.venv
project/lang
env/
venv/
ENV/
Expand Down
36 changes: 36 additions & 0 deletions project/lang.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
grammar lang;

WS: [ \r\n\t]+ -> skip;

prog: stmt* EOF;

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: '0' | ([1-9] [0-9]*);
CHAR: '"' [a-z] '"';
49 changes: 49 additions & 0 deletions project/task11.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from project.lang.langLexer import langLexer
from project.lang.langParser import langParser
from project.lang.langListener import langListener

from antlr4 import ParserRuleContext, CommonTokenStream
from antlr4.InputStream import InputStream


class NodeCountListener(langListener):

def __init__(self) -> None:
super(langListener, self).__init__()
self.count = 0

def enterEveryRule(self, ctx):
self.count += 1

def get_count(self):
return self.count


class StringifyListener(langListener):

def __init__(self):
super(langListener, self).__init__()
self.result = ""

def enterEveryRule(self, rule):
self.result += rule.getText()

def get_result(self):
return self.result


def prog_to_tree(program: str) -> tuple[ParserRuleContext, bool]:
parser = langParser(CommonTokenStream(langLexer(InputStream(program))))
return parser.prog(), (parser.getNumberOfSyntaxErrors() == 0)


def nodes_count(tree: ParserRuleContext) -> int:
listener = NodeCountListener()
tree.enterRule(listener)
return listener.get_count()


def tree_to_prog(tree: ParserRuleContext) -> str:
listener = StringifyListener()
tree.enterRule(listener)
return listener.get_result()
2 changes: 1 addition & 1 deletion project/task7.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def cfpq_with_matrix(
(p.body[0].to_text(), p.body[1].to_text())
)

for i in range(graph.number_of_nodes() ** 2):
for j in range(graph.number_of_nodes() ** 2):
for N, NN in N_to_NN.items():
for Nl, Nr in NN:
M_new[N] += M[Nl] @ M[Nr]
Expand Down
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
Binary file modified tests/.DS_Store
Binary file not shown.
Binary file added tests/autotests/.DS_Store
Binary file not shown.
35 changes: 0 additions & 35 deletions tests/test_task_1.py

This file was deleted.

0 comments on commit 2de9053

Please sign in to comment.