-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[HA-11.0] Add initial sollution for 11th task
- Loading branch information
Showing
4 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters