Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
7eb5c9a5
Commit
7eb5c9a5
authored
Oct 29, 2010
by
Robert Griesemer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ebnf: use append
R=r, rsc CC=golang-dev
https://golang.org/cl/2799041
parent
75855a8f
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
21 additions
and
29 deletions
+21
-29
ebnf.go
src/pkg/ebnf/ebnf.go
+10
-6
parser.go
src/pkg/ebnf/parser.go
+11
-23
No files found.
src/pkg/ebnf/ebnf.go
View file @
7eb5c9a5
...
...
@@ -23,7 +23,6 @@
package
ebnf
import
(
"container/vector"
"go/scanner"
"go/token"
"os"
...
...
@@ -123,7 +122,7 @@ func isLexical(name string) bool {
type
verifier
struct
{
scanner
.
ErrorVector
worklist
vector
.
Vector
worklist
[]
*
Production
reached
Grammar
// set of productions reached from (and including) the root production
grammar
Grammar
}
...
...
@@ -132,7 +131,7 @@ type verifier struct {
func
(
v
*
verifier
)
push
(
prod
*
Production
)
{
name
:=
prod
.
Name
.
String
if
_
,
found
:=
v
.
reached
[
name
];
!
found
{
v
.
worklist
.
Push
(
prod
)
v
.
worklist
=
append
(
v
.
worklist
,
prod
)
v
.
reached
[
name
]
=
prod
}
}
...
...
@@ -205,14 +204,19 @@ func (v *verifier) verify(grammar Grammar, start string) {
// initialize verifier
v
.
ErrorVector
.
Reset
()
v
.
worklist
.
Resize
(
0
,
0
)
v
.
worklist
=
v
.
worklist
[
0
:
0
]
v
.
reached
=
make
(
Grammar
)
v
.
grammar
=
grammar
// work through the worklist
v
.
push
(
root
)
for
v
.
worklist
.
Len
()
>
0
{
prod
:=
v
.
worklist
.
Pop
()
.
(
*
Production
)
for
{
n
:=
len
(
v
.
worklist
)
-
1
if
n
<
0
{
break
}
prod
:=
v
.
worklist
[
n
]
v
.
worklist
=
v
.
worklist
[
0
:
n
]
v
.
verifyExpr
(
prod
.
Expr
,
isLexical
(
prod
.
Name
.
String
))
}
...
...
src/pkg/ebnf/parser.go
View file @
7eb5c9a5
...
...
@@ -5,7 +5,6 @@
package
ebnf
import
(
"container/vector"
"go/scanner"
"go/token"
"os"
...
...
@@ -116,36 +115,30 @@ func (p *parser) parseTerm() (x Expression) {
func
(
p
*
parser
)
parseSequence
()
Expression
{
var
list
vector
.
Vector
var
list
Sequence
for
x
:=
p
.
parseTerm
();
x
!=
nil
;
x
=
p
.
parseTerm
()
{
list
.
Push
(
x
)
list
=
append
(
list
,
x
)
}
// no need for a sequence if list.Len() < 2
switch
l
ist
.
Len
(
)
{
switch
l
en
(
list
)
{
case
0
:
return
nil
case
1
:
return
list
.
At
(
0
)
.
(
Expression
)
return
list
[
0
]
}
// convert list into a sequence
seq
:=
make
(
Sequence
,
list
.
Len
())
for
i
:=
0
;
i
<
list
.
Len
();
i
++
{
seq
[
i
]
=
list
.
At
(
i
)
.
(
Expression
)
}
return
seq
return
list
}
func
(
p
*
parser
)
parseExpression
()
Expression
{
var
list
vector
.
Vector
var
list
Alternative
for
{
x
:=
p
.
parseSequence
()
if
x
!=
nil
{
list
.
Push
(
x
)
if
x
:=
p
.
parseSequence
();
x
!=
nil
{
list
=
append
(
list
,
x
)
}
if
p
.
tok
!=
token
.
OR
{
break
...
...
@@ -154,19 +147,14 @@ func (p *parser) parseExpression() Expression {
}
// no need for an Alternative node if list.Len() < 2
switch
l
ist
.
Len
(
)
{
switch
l
en
(
list
)
{
case
0
:
return
nil
case
1
:
return
list
.
At
(
0
)
.
(
Expression
)
return
list
[
0
]
}
// convert list into an Alternative node
alt
:=
make
(
Alternative
,
list
.
Len
())
for
i
:=
0
;
i
<
list
.
Len
();
i
++
{
alt
[
i
]
=
list
.
At
(
i
)
.
(
Expression
)
}
return
alt
return
list
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a 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