nitlanguage
/
nit.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
nitcc: collect items with conflict (now that there is always a resolution attempt)
[nit.git]
/
contrib
/
nitcc
/
src
/
nitcc.nit
diff --git
a/contrib/nitcc/src/nitcc.nit
b/contrib/nitcc/src/nitcc.nit
index
c0b522f
..
7e380e1
100644
(file)
--- a/
contrib/nitcc/src/nitcc.nit
+++ b/
contrib/nitcc/src/nitcc.nit
@@
-27,7
+27,7
@@
var fi = args.first
var text
if fi != "-" then
var text
if fi != "-" then
- var f = new IFStream.open(fi)
+ var f = new FileReader.open(fi)
text = f.read_all
f.close
else
text = f.read_all
f.close
else
@@
-46,7
+46,7
@@
var node = p.parse
if not node isa NProd then
assert node isa NError
if not node isa NProd then
assert node isa NError
- print "{node.position} Syntax Error: {node.message}"
+ print "{node.position.to_s} Syntax Error: {node.message}"
exit 1
abort
end
exit 1
abort
end
@@
-72,8
+72,8
@@
end
var lr = gram.lr0
var conflitcs = new ArraySet[Production]
var lr = gram.lr0
var conflitcs = new ArraySet[Production]
-for s in lr.states do for t, a in s.guarded_reduce do if a.length > 1 or s.guarded_shift.has_key(t) then
- for i in a do conflitcs.add(i.alt.prod)
+for s in lr.states do
+ for i in s.conflicting_items do conflitcs.add(i.alt.prod)
end
if not conflitcs.is_empty then
end
if not conflitcs.is_empty then
@@
-91,18
+91,18
@@
end
var nbalts = 0
for prod in gram.prods do nbalts += prod.alts.length
var nbalts = 0
for prod in gram.prods do nbalts += prod.alts.length
-print "Concrete grammar: {gram.prods.length} productions, {nbalts} alternatives (see {name}.concrete_grammar.txt)"
+print "Concrete grammar: {gram.prods.length} productions, {nbalts} alternatives (see {name}.concrete_grammar.out)"
var pretty = gram.pretty
var pretty = gram.pretty
-var f = new OFStream.open("{name}.concrete_grammar.txt")
+var f = new FileWriter.open("{name}.concrete_grammar.out")
f.write "// Concrete grammar of {name}\n"
f.write pretty
f.close
f.write "// Concrete grammar of {name}\n"
f.write pretty
f.close
-print "LR automaton: {lr.states.length} states (see {name}.lr.dot and {name}.lr.txt)"
+print "LR automaton: {lr.states.length} states (see {name}.lr.dot and {name}.lr.out)"
lr.to_dot("{name}.lr.dot")
pretty = lr.pretty
lr.to_dot("{name}.lr.dot")
pretty = lr.pretty
-f = new OFStream.open("{name}.lr.txt")
+f = new FileWriter.open("{name}.lr.out")
f.write "// LR automaton of {name}\n"
f.write pretty
f.close
f.write "// LR automaton of {name}\n"
f.write pretty
f.close
@@
-113,19
+113,28
@@
var nfa = v2.nfa
print "NFA automaton: {nfa.states.length} states (see {name}.nfa.dot)"
nfa.to_dot("{name}.nfa.dot")
print "NFA automaton: {nfa.states.length} states (see {name}.nfa.dot)"
nfa.to_dot("{name}.nfa.dot")
-var dfa = nfa.to_dfa
+var dfa = nfa.to_dfa.to_minimal_dfa
+
+dfa.solve_token_inclusion
+
+print "DFA automaton: {dfa.states.length} states (see {name}.dfa.dot)"
+dfa.to_dot("{name}.dfa.dot")
+
if dfa.tags.has_key(dfa.start) then
print "Error: Empty tokens {dfa.tags[dfa.start].join(" ")}"
exit(1)
end
if dfa.tags.has_key(dfa.start) then
print "Error: Empty tokens {dfa.tags[dfa.start].join(" ")}"
exit(1)
end
-dfa.solve_token_inclusion
for s, tks in dfa.tags do
if tks.length <= 1 then continue
print "Error: Conflicting tokens: {tks.join(" ")}"
exit(1)
end
for s, tks in dfa.tags do
if tks.length <= 1 then continue
print "Error: Conflicting tokens: {tks.join(" ")}"
exit(1)
end
-print "DFA automaton: {dfa.states.length} states (see {name}.dfa.dot)"
-dfa.to_dot("{name}.dfa.dot")
+for t in gram.tokens do
+ if t.name == "Eof" then continue
+ if dfa.retrotags.has_key(t) and not dfa.retrotags[t].is_empty then continue
+ print "Error: Token {t} matches nothing"
+ exit(1)
+end
# Generate Nit code
# Generate Nit code
@@
-133,11
+142,16
@@
print "Generate {name}_lexer.nit {name}_parser.nit {name}_test_parser.nit"
dfa.gen_to_nit("{name}_lexer.nit", name, "{name}_parser")
lr.gen_to_nit("{name}_parser.nit", name)
dfa.gen_to_nit("{name}_lexer.nit", name, "{name}_parser")
lr.gen_to_nit("{name}_parser.nit", name)
-f = new OFStream.open("{name}_test_parser.nit")
+f = new FileWriter.open("{name}_test_parser.nit")
f.write """# Generated by nitcc for the language {{{name}}}
f.write """# Generated by nitcc for the language {{{name}}}
+
+# Standalone parser tester for the language {{{name}}}
+module {{{name}}}_test_parser
import nitcc_runtime
import {{{name}}}_lexer
import {{{name}}}_parser
import nitcc_runtime
import {{{name}}}_lexer
import {{{name}}}_parser
+
+# Class to test the parser for the language {{{name}}}
class TestParser_{{{name}}}
super TestParser
redef fun name do return \"{{{name}}}\"
class TestParser_{{{name}}}
super TestParser
redef fun name do return \"{{{name}}}\"