Skip to content

Commit

Permalink
Merge branch 'visitor-test'
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielUH2019 committed Jan 7, 2023
2 parents b22b8bd + fa2b3d7 commit 93fcab2
Show file tree
Hide file tree
Showing 11 changed files with 783 additions and 737 deletions.
2 changes: 1 addition & 1 deletion automata.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pydot
from typing import Dict, List, Self, Tuple
from typing import Dict, List, Tuple
from abc import ABC, abstractmethod
from lexer.int_generator import generator
from parser.utils import ContainerSet
Expand Down
35 changes: 23 additions & 12 deletions grammar.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
from typing import Self
# from typing import Self


class Symbol(object):
Expand All @@ -15,7 +15,8 @@ def __repr__(self):
return repr(self.Name)

def __add__(self, other):
if isinstance(other, Terminal|NonTerminal):
if isinstance(other, Terminal) or isinstance(other, NonTerminal):
# if isinstance(other, Terminal|NonTerminal):
return Sentence(self, other)

raise TypeError(other)
Expand Down Expand Up @@ -57,15 +58,17 @@ def __imod__(self, other):
assert len(other) == len(other[0]) + 2, "Debe definirse una, y solo una, regla por cada símbolo de la producción"
# assert len(other) == 2, "Tiene que ser una Tupla de 2 elementos (sentence, attribute)"

if isinstance(other[0], Terminal|NonTerminal) or isinstance(other[0], Sentence):
if isinstance(other[0], Terminal) or isinstance(other[0], NonTerminal) or isinstance(other[0], Sentence):
# if isinstance(other[0], Terminal|NonTerminal) or isinstance(other[0], Sentence):
p = AttributeProduction(self, other[0], other[1:])
else:
raise Exception("")

self.Grammar.Add_Production(p)
return self

if isinstance(other, Terminal|NonTerminal):
if isinstance(other):
# if isinstance(other, Terminal|NonTerminal):
p = Production(self, Sentence(other))
self.Grammar.Add_Production(p)
return self
Expand Down Expand Up @@ -116,15 +119,17 @@ def __init__(self, grammar: 'Grammar'):

class Sentence(object):

def __init__(self, *args: Terminal|NonTerminal):
def __init__(self, *args):
# def __init__(self, *args: Terminal|NonTerminal):
self._symbols = tuple(x for x in args if not x.IsEpsilon)
self.hash = hash(self._symbols)

def __len__(self):
return len(self._symbols)

def __add__(self, other):
if isinstance(other, Terminal|NonTerminal):
if isinstance(other, Terminal) or isinstance(other, NonTerminal):
# if isinstance(other, Terminal|NonTerminal):
return Sentence(*(self._symbols + (other,)))

if isinstance(other, Sentence):
Expand All @@ -136,7 +141,8 @@ def __or__(self, other):
if isinstance(other, Sentence):
return SentenceList(self, other)

if isinstance(other, Terminal|NonTerminal):
if isinstance(other):
# if isinstance(other, Terminal|NonTerminal):
return SentenceList(self, Sentence(other))

raise TypeError(other)
Expand All @@ -153,7 +159,8 @@ def __iter__(self):
def __getitem__(self, index):
return self._symbols[index]

def __eq__(self, other: Self):
def __eq__(self, other):
# def __eq__(self, other: Self):
return self._symbols == other._symbols

def __hash__(self):
Expand Down Expand Up @@ -182,7 +189,8 @@ def __or__(self, other):
self.Add(other)
return self

if isinstance(other, Terminal|NonTerminal):
if isinstance(other):
# if isinstance(other, Terminal|NonTerminal):
return self | Sentence(other)


Expand Down Expand Up @@ -247,7 +255,8 @@ def IsEpsilon(self):

class AttributeProduction(Production):

def __init__(self, nonTerminal: NonTerminal, sentence: Sentence|Terminal|NonTerminal, attributes):
# def __init__(self, nonTerminal: NonTerminal, sentence: Sentence|Terminal|NonTerminal, attributes):
def __init__(self, nonTerminal: NonTerminal, sentence, attributes):
if not isinstance(sentence, Sentence) and isinstance(sentence, Symbol):
sentence = Sentence(sentence)
super(AttributeProduction, self).__init__(nonTerminal, sentence)
Expand Down Expand Up @@ -280,7 +289,8 @@ def __init__(self):
self.Productions: list[Production] = []
self.nonTerminals: list[NonTerminal] = []
self.terminals: list[Terminal] = []
self.startSymbol: NonTerminal | None = None
# self.startSymbol: NonTerminal | None = None
self.startSymbol = None
# production type
self.pType = None
self.Epsilon = Epsilon(self)
Expand Down Expand Up @@ -463,7 +473,8 @@ def __repr__(self):
return str(self)


def __eq__(self, other: Self):
def __eq__(self, other):
# def __eq__(self, other: Self):
return (
(self.pos == other.pos) and
(self.production == other.production) and
Expand Down
6 changes: 4 additions & 2 deletions parser/ll_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ def build_parser(self, grammar: Grammar):
# print('parsing table ', parsing_table)
def parser(w: list[Terminal]) -> list[Production]:
assert grammar.startSymbol is not None, 'Start symbol cannot be None'
stack: list[NonTerminal|Terminal] = [grammar.startSymbol]
stack = [grammar.startSymbol]
# stack: list[NonTerminal|Terminal] = [grammar.startSymbol]
cursor = 0
output: list[Production] = []
# parsing w...
Expand Down Expand Up @@ -90,7 +91,8 @@ def __build_ast(self, production: AttributeProduction, left_parse, tokens, inher
# print('Node build', P(inherited,synteticed) if P is not None else None)
return P(inherited,synteticed) if P is not None else None

def build_parsing_table(self, G: Grammar, firsts: dict[Symbol | Sentence, ContainerSet], follows: dict[Symbol | Sentence, ContainerSet]):
def build_parsing_table(self, G: Grammar, firsts, follows):
# def build_parsing_table(self, G: Grammar, firsts: dict[Symbol | Sentence, ContainerSet], follows: dict[Symbol | Sentence, ContainerSet]):
# init parsing table
M: dict[tuple[NonTerminal, Terminal], list[Production]] = {}

Expand Down
9 changes: 7 additions & 2 deletions parser/tzscript_ast.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

class Node:
def accept(self, visitor):
pass
Expand Down Expand Up @@ -70,7 +71,7 @@ def __init__(self, idx, typex):
self.id = idx
self.type = typex
def accept(self, visitor):
return visitor.visit_attr_declaraion_node(self)
return visitor.visit_attr_declaration_node(self)


class AtomicNode(ExpressionNode):
Expand Down Expand Up @@ -163,7 +164,11 @@ def accept(self, visitor):
return visitor.visit_false_node(self)

class ConstantNumNode(AtomicNode):
pass
def __init__(self, lex):
self.lex = lex
self.type = 'num'
def accept(self, visitor):
return visitor.visit_constant_num_node(self)

class VariableNode(AtomicNode):
pass
Expand Down
19 changes: 11 additions & 8 deletions parser/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Self
# from typing import Self

from grammar import Grammar, Sentence, Symbol

Expand All @@ -24,15 +24,15 @@ def set_epsilon(self, value=True):
self.contains_epsilon = value
return last != self.contains_epsilon

def update(self, other: Self):
def update(self, other):
n = len(self.set)
self.set.update(other.set)
return n != len(self.set)

def epsilon_update(self, other: Self):
def epsilon_update(self, other):
return self.set_epsilon(self.contains_epsilon | other.contains_epsilon)

def hard_update(self, other: Self):
def hard_update(self, other):
return self.update(other) | self.epsilon_update(other)

def find_match(self, match):
Expand All @@ -56,7 +56,7 @@ def __iter__(self):
def __nonzero__(self):
return len(self) > 0

def __eq__(self, other: Self):
def __eq__(self, other):
if isinstance(other, set):
return self.set == other
return isinstance(other, ContainerSet) and self.set == other.set and self.contains_epsilon == other.contains_epsilon
Expand Down Expand Up @@ -96,7 +96,8 @@ def compute_local_first(firsts, alpha: Sentence):
# Computes First(Vt) U First(Vn) U First(alpha)
# P: X -> alpha
def compute_firsts(G: Grammar):
firsts: dict[Symbol | Sentence, ContainerSet] = {}
firsts = {}
# firsts: dict[Symbol | Sentence, ContainerSet] = {}
change = True

# init First(Vt)
Expand Down Expand Up @@ -134,8 +135,10 @@ def compute_firsts(G: Grammar):
# First(Vt) + First(Vt) + First(RightSides)
return firsts

def compute_follows(G: Grammar, firsts: dict[Symbol | Sentence, ContainerSet]):
follows: dict[Symbol|Sentence, ContainerSet] = { }
def compute_follows(G: Grammar, firsts):
# def compute_follows(G: Grammar, firsts: dict[Symbol | Sentence, ContainerSet]):
follows = { }
# follows: dict[Symbol|Sentence, ContainerSet] = { }
change = True

# init Follow(Vn)
Expand Down
Loading

0 comments on commit 93fcab2

Please sign in to comment.