Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
fja
eval
Commits
c99829ee
Commit
c99829ee
authored
Mar 07, 2021
by
Vladimír Štill
Browse files
lib: Improve error messages for grammars
parent
bce0026f
Pipeline
#68021
passed with stage
in 51 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
lib/grammars.py
View file @
c99829ee
...
...
@@ -25,7 +25,7 @@ class RegGrammar:
@
staticmethod
def
from_cfg
(
cfg
:
CFG
)
->
RegGrammar
:
assert
cfg
.
is_regular
(
)
cfg
.
check_regular
(
throw
=
True
)
reg_rules
:
RegGrammar
.
Rules
=
{}
for
nonterminal
in
cfg
.
rules
:
for
rule
in
cfg
.
rules
[
nonterminal
]:
...
...
@@ -135,4 +135,4 @@ class RegGrammar:
if
isinstance
(
rule
,
Tuple
)
and
rule
[
1
]
not
in
grammar
.
nonterminals
:
grammar
.
rules
[
nonterminal
].
remove
(
rule
)
return
grammar
\ No newline at end of file
return
grammar
lib/grammars_cfg.py
View file @
c99829ee
...
...
@@ -23,6 +23,11 @@ def any_of(pred: Callable[[T], bool], it: Iterable[T]) -> bool:
return
any
(
map
(
pred
,
it
))
class
NotRegularException
(
Exception
):
def
__init__
(
self
,
message
:
str
)
->
None
:
super
().
__init__
(
f
"Gramatika není regulární:
{
message
}
"
)
class
GeneratesResult
:
def
__init__
(
self
,
value
:
bool
,
cnf_cfg
:
CFG
,
cyk_table
:
Optional
[
List
[
List
[
Set
[
Nonterminal
]]]]
=
None
):
...
...
@@ -155,22 +160,26 @@ class CFG:
return
True
return
False
def
is
_regular
(
self
)
->
bool
:
def
check
_regular
(
self
,
throw
:
bool
=
False
)
->
bool
:
for
nonterminal
in
self
.
rules
:
for
rule
in
self
.
rules
[
nonterminal
]:
if
isinstance
(
rule
,
Eps
):
if
nonterminal
==
self
.
init
and
not
self
.
is_nonterminal_used
(
nonterminal
):
continue
else
:
#print("Gramatika není regulární: výskyt epsilon u neterminálu, který se vyskytuje na pravé straně některého pravidla.")
return
False
if
throw
:
raise
NotRegularException
(
"výskyt epsilon u neterminálu, který se "
"vyskytuje na pravé straně některého pravidla."
)
return
False
if
isinstance
(
rule
,
tuple
):
# TODO nicer
if
(
len
(
rule
)
==
1
and
isinstance
(
rule
[
0
],
Terminal
))
or
\
(
len
(
rule
)
==
2
and
isinstance
(
rule
[
0
],
Terminal
)
and
isinstance
(
rule
[
1
],
Nonterminal
)):
continue
#print("Gramatika není regulární: špatný tvar pravé strany některého z pravidel.")
if
throw
:
raise
NotRegularException
(
"špatný tvar pravé strany některého z pravidel."
)
return
False
return
True
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment