Commit 054354eb authored by Robert Griesemer's avatar Robert Griesemer

gofmt src/cmd/goyacc

(with this change: gofmt -l src/cmd/ | wc
is 0 0 0)

R=ken, rsc
http://go/go-review/1024005
parent 79284cae
...@@ -44,8 +44,7 @@ package main ...@@ -44,8 +44,7 @@ package main
// major difference is lack of stem ("y" variable) // major difference is lack of stem ("y" variable)
// //
import import (
(
"flag"; "flag";
"fmt"; "fmt";
"bufio"; "bufio";
...@@ -54,66 +53,61 @@ import ...@@ -54,66 +53,61 @@ import
// the following are adjustable // the following are adjustable
// according to memory size // according to memory size
const const (
(
ACTSIZE = 30000; ACTSIZE = 30000;
NSTATES = 2000; NSTATES = 2000;
TEMPSIZE = 2000; TEMPSIZE = 2000;
SYMINC = 50; // increase for non-term or term SYMINC = 50; // increase for non-term or term
RULEINC = 50; // increase for max rule length prodptr[i] RULEINC = 50; // increase for max rule length prodptr[i]
PRODINC = 100; // increase for productions prodptr PRODINC = 100; // increase for productions prodptr
WSETINC = 50; // increase for working sets wsets WSETINC = 50; // increase for working sets wsets
STATEINC = 200; // increase for states statemem STATEINC = 200; // increase for states statemem
NAMESIZE = 50; NAMESIZE = 50;
NTYPES = 63; NTYPES = 63;
ISIZE = 400; ISIZE = 400;
PRIVATE = 0xE000; // unicode private use PRIVATE = 0xE000; // unicode private use
// relationships which must hold: // relationships which must hold:
// TEMPSIZE >= NTERMS + NNONTERM + 1; // TEMPSIZE >= NTERMS + NNONTERM + 1;
// TEMPSIZE >= NSTATES; // TEMPSIZE >= NSTATES;
// //
NTBASE = 010000; NTBASE = 010000;
ERRCODE = 8190; ERRCODE = 8190;
ACCEPTCODE = 8191; ACCEPTCODE = 8191;
YYLEXUNK = 3; YYLEXUNK = 3;
TOKSTART = 4; //index of first defined token TOKSTART = 4; //index of first defined token
) )
// no, left, right, binary assoc. // no, left, right, binary assoc.
const const (
( NOASC = iota;
NOASC = iota;
LASC; LASC;
RASC; RASC;
BASC; BASC;
) )
// flags for state generation // flags for state generation
const const (
( DONE = iota;
DONE = iota;
MUSTDO; MUSTDO;
MUSTLOOKAHEAD; MUSTLOOKAHEAD;
) )
// flags for a rule having an action, and being reduced // flags for a rule having an action, and being reduced
const const (
( ACTFLAG = 1<<(iota+2);
ACTFLAG = 1<<(iota+2);
REDFLAG; REDFLAG;
) )
// output parser flags // output parser flags
const YYFLAG = -1000 const YYFLAG = -1000
// parse tokens // parse tokens
const const (
(
IDENTIFIER = PRIVATE+iota; IDENTIFIER = PRIVATE+iota;
MARK; MARK;
TERM; TERM;
...@@ -130,246 +124,222 @@ const ...@@ -130,246 +124,222 @@ const
UNION; UNION;
) )
const ENDFILE = 0 const ENDFILE = 0
const EMPTY = 1 const EMPTY = 1
const WHOKNOWS = 0 const WHOKNOWS = 0
const OK = 1 const OK = 1
const NOMORE = -1000 const NOMORE = -1000
// macros for getting associativity and precedence levels // macros for getting associativity and precedence levels
func func ASSOC(i int) int {
ASSOC(i int) int return i&3;
{
return i & 3;
} }
func func PLEVEL(i int) int {
PLEVEL(i int) int return (i>>4)&077;
{
return (i >> 4) & 077;
} }
func func TYPE(i int) int {
TYPE(i int) int return (i>>10)&077;
{
return (i >> 10) & 077;
} }
// macros for setting associativity and precedence levels // macros for setting associativity and precedence levels
func func SETASC(i, j int) int {
SETASC(i, j int) int return i|j;
{
return i | j;
} }
func func SETPLEV(i, j int) int {
SETPLEV(i, j int) int return i|(j<<4);
{
return i | (j << 4);
} }
func func SETTYPE(i, j int) int {
SETTYPE(i, j int) int return i|(j<<10);
{
return i | (j << 10);
} }
// I/O descriptors // I/O descriptors
var finput *bufio.Reader // input file var finput *bufio.Reader // input file
var stderr *bufio.Writer var stderr *bufio.Writer
var ftable *bufio.Writer // y.go file var ftable *bufio.Writer // y.go file
var foutput *bufio.Writer // y.output file var foutput *bufio.Writer // y.output file
var oflag string // -o [y.go] - y.go file var oflag string // -o [y.go] - y.go file
var vflag string // -v [y.output] - y.output file var vflag string // -v [y.output] - y.output file
var lflag bool // -l - disable line directives var lflag bool // -l - disable line directives
var stacksize = 200 var stacksize = 200
// communication variables between various I/O routines // communication variables between various I/O routines
var infile string // input file name var infile string // input file name
var numbval int // value of an input number var numbval int // value of an input number
var tokname string // input token name, slop for runes and 0 var tokname string // input token name, slop for runes and 0
var tokflag = false; var tokflag = false
// structure declarations // structure declarations
type Lkset []int type Lkset []int
type Pitem type Pitem struct {
struct prod []int;
{ off int; // offset within the production
prod []int; first int; // first term or non-term in item
off int; // offset within the production prodno int; // production number for sorting
first int; // first term or non-term in item
prodno int; // production number for sorting
} }
type Item type Item struct {
struct pitem Pitem;
{ look Lkset;
pitem Pitem;
look Lkset;
} }
type Symb type Symb struct {
struct name string;
{ value int;
name string;
value int;
} }
type Wset type Wset struct {
struct pitem Pitem;
{ flag int;
pitem Pitem; ws Lkset;
flag int;
ws Lkset;
} }
// storage of types // storage of types
var ntypes int // number of types defined var ntypes int // number of types defined
var typeset [NTYPES]string // pointers to type tags var typeset [NTYPES]string // pointers to type tags
// token information // token information
var ntokens = 0 // number of tokens var ntokens = 0 // number of tokens
var tokset []Symb var tokset []Symb
var toklev []int // vector with the precedence of the terminals var toklev []int // vector with the precedence of the terminals
// nonterminal information // nonterminal information
var nnonter = -1 // the number of nonterminals var nnonter = -1 // the number of nonterminals
var nontrst []Symb var nontrst []Symb
var start int // start symbol var start int // start symbol
// state information // state information
var nstate = 0 // number of states var nstate = 0 // number of states
var pstate = make([]int, NSTATES+2) // index into statemem to the descriptions of the states var pstate = make([]int, NSTATES+2) // index into statemem to the descriptions of the states
var statemem []Item var statemem []Item
var tystate = make([]int, NSTATES) // contains type information about the states var tystate = make([]int, NSTATES) // contains type information about the states
var tstates []int // states generated by terminal gotos var tstates []int // states generated by terminal gotos
var ntstates []int // states generated by nonterminal gotos var ntstates []int // states generated by nonterminal gotos
var mstates = make([]int, NSTATES) // chain of overflows of term/nonterm generation lists var mstates = make([]int, NSTATES) // chain of overflows of term/nonterm generation lists
var lastred int // number of last reduction of a state var lastred int // number of last reduction of a state
var defact = make([]int, NSTATES) // default actions of states var defact = make([]int, NSTATES) // default actions of states
// lookahead set information // lookahead set information
var lkst []Lkset var lkst []Lkset
var nolook = 0 // flag to turn off lookahead computations var nolook = 0 // flag to turn off lookahead computations
var tbitset = 0 // size of lookahead sets var tbitset = 0 // size of lookahead sets
var clset Lkset // temporary storage for lookahead computations var clset Lkset // temporary storage for lookahead computations
// working set information // working set information
var wsets []Wset var wsets []Wset
var cwp int var cwp int
// storage for action table // storage for action table
var amem []int // action table storage var amem []int // action table storage
var memp int // next free action table position var memp int // next free action table position
var indgo = make([]int, NSTATES) // index to the stored goto table var indgo = make([]int, NSTATES) // index to the stored goto table
// temporary vector, indexable by states, terms, or ntokens // temporary vector, indexable by states, terms, or ntokens
var temp1 = make([]int, TEMPSIZE) // temporary storage, indexed by terms + ntokens or states var temp1 = make([]int, TEMPSIZE) // temporary storage, indexed by terms + ntokens or states
var lineno = 1 // current input line number var lineno = 1 // current input line number
var fatfl = 1 // if on, error is fatal var fatfl = 1 // if on, error is fatal
var nerrors = 0 // number of errors var nerrors = 0 // number of errors
// assigned token type values // assigned token type values
var extval = 0 var extval = 0
// grammar rule information // grammar rule information
var nprod = 1 // number of productions var nprod = 1 // number of productions
var prdptr [][]int // pointers to descriptions of productions var prdptr [][]int // pointers to descriptions of productions
var levprd []int // precedence levels for the productions var levprd []int // precedence levels for the productions
var rlines []int // line number for this rule var rlines []int // line number for this rule
// statistics collection variables // statistics collection variables
var zzgoent = 0 var zzgoent = 0
var zzgobest = 0 var zzgobest = 0
var zzacent = 0 var zzacent = 0
var zzexcp = 0 var zzexcp = 0
var zzclose = 0 var zzclose = 0
var zzrrconf = 0 var zzrrconf = 0
var zzsrconf = 0 var zzsrconf = 0
var zzstate = 0 var zzstate = 0
// optimizer arrays // optimizer arrays
var yypgo [][]int var yypgo [][]int
var optst [][]int var optst [][]int
var ggreed []int var ggreed []int
var pgo []int var pgo []int
var maxspr int // maximum spread of any entry var maxspr int // maximum spread of any entry
var maxoff int // maximum offset into a array var maxoff int // maximum offset into a array
var maxa int var maxa int
// storage for information about the nonterminals // storage for information about the nonterminals
var pres [][][]int // vector of pointers to productions yielding each nonterminal var pres [][][]int // vector of pointers to productions yielding each nonterminal
var pfirst []Lkset var pfirst []Lkset
var pempty []int // vector of nonterminals nontrivially deriving e var pempty []int // vector of nonterminals nontrivially deriving e
// random stuff picked out from between functions // random stuff picked out from between functions
var indebug = 0 // debugging flag for cpfir var indebug = 0 // debugging flag for cpfir
var pidebug = 0 // debugging flag for putitem var pidebug = 0 // debugging flag for putitem
var gsdebug = 0 // debugging flag for stagen var gsdebug = 0 // debugging flag for stagen
var cldebug = 0 // debugging flag for closure var cldebug = 0 // debugging flag for closure
var pkdebug = 0 // debugging flag for apack var pkdebug = 0 // debugging flag for apack
var g2debug = 0 // debugging for go2gen var g2debug = 0 // debugging for go2gen
var adb = 0 // debugging for callopt var adb = 0 // debugging for callopt
type Resrv type Resrv struct {
struct
{
name string; name string;
value int; value int;
} }
var resrv = var resrv = []Resrv{
[]Resrv { Resrv{"binary", BINARY},
Resrv{"binary", BINARY}, Resrv{"left", LEFT},
Resrv{"left", LEFT}, Resrv{"nonassoc", BINARY},
Resrv{"nonassoc", BINARY}, Resrv{"prec", PREC},
Resrv{"prec", PREC}, Resrv{"right", RIGHT},
Resrv{"right", RIGHT}, Resrv{"start", START},
Resrv{"start", START}, Resrv{"term", TERM},
Resrv{"term", TERM}, Resrv{"token", TERM},
Resrv{"token", TERM}, Resrv{"type", TYPEDEF},
Resrv{"type", TYPEDEF}, Resrv{"union", UNION},
Resrv{"union", UNION}, Resrv{"struct", UNION},
Resrv{"struct", UNION}
} }
var zznewstate = 0 var zznewstate = 0
const EOF = -1
const UTFmax = 0x3f
func const EOF = -1
main() const UTFmax = 0x3f
{
setup(); // initialize and read productions func main() {
setup(); // initialize and read productions
tbitset = (ntokens+32)/32; tbitset = (ntokens+32)/32;
cpres(); // make table of which productions yield a given nonterminal cpres(); // make table of which productions yield a given nonterminal
cempty(); // make a table of which nonterminals can match the empty string cempty(); // make a table of which nonterminals can match the empty string
cpfir(); // make a table of firsts of nonterminals cpfir(); // make a table of firsts of nonterminals
stagen(); // generate the states stagen(); // generate the states
yypgo = make([][]int, nnonter+1); yypgo = make([][]int, nnonter+1);
optst = make([][]int, nstate); optst = make([][]int, nstate);
output(); // write the states and the tables output(); // write the states and the tables
go2out(); go2out();
hideprod(); hideprod();
...@@ -382,9 +352,7 @@ main() ...@@ -382,9 +352,7 @@ main()
exit(0); exit(0);
} }
func func setup() {
setup()
{
var j, ty int; var j, ty int;
stderr = bufio.NewWriter(os.NewFile(2, "stderr")); stderr = bufio.NewWriter(os.NewFile(2, "stderr"));
...@@ -414,13 +382,13 @@ setup() ...@@ -414,13 +382,13 @@ setup()
t := gettok(); t := gettok();
outer: outer:
for { for {
switch t { switch t {
default: default:
error("syntax error tok=%v", t-PRIVATE); error("syntax error tok=%v", t-PRIVATE);
case MARK,ENDFILE: case MARK, ENDFILE:
break outer; break outer;
case ';': case ';':
...@@ -445,18 +413,20 @@ setup() ...@@ -445,18 +413,20 @@ setup()
t = chfind(1, tokname); t = chfind(1, tokname);
if t < NTBASE { if t < NTBASE {
j = TYPE(toklev[t]); j = TYPE(toklev[t]);
if(j != 0 && j != ty) { if j != 0 && j != ty {
error("type redeclaration of token ", error("type redeclaration of token ",
tokset[t].name); tokset[t].name);
} else } else {
toklev[t] = SETTYPE(toklev[t], ty); toklev[t] = SETTYPE(toklev[t], ty);
}
} else { } else {
j = nontrst[t-NTBASE].value; j = nontrst[t-NTBASE].value;
if(j != 0 && j != ty) { if j != 0 && j != ty {
error("type redeclaration of nonterminal %v", error("type redeclaration of nonterminal %v",
nontrst[t-NTBASE].name); nontrst[t-NTBASE].name);
} else } else {
nontrst[t-NTBASE].value = ty; nontrst[t-NTBASE].value = ty;
}
} }
continue; continue;
...@@ -470,7 +440,7 @@ setup() ...@@ -470,7 +440,7 @@ setup()
case UNION: case UNION:
cpyunion(); cpyunion();
case LEFT,BINARY,RIGHT,TERM: case LEFT, BINARY, RIGHT, TERM:
// nonzero means new prec. and assoc. // nonzero means new prec. and assoc.
lev := t-TERM; lev := t-TERM;
if lev != 0 { if lev != 0 {
...@@ -492,7 +462,7 @@ setup() ...@@ -492,7 +462,7 @@ setup()
t = gettok(); t = gettok();
continue; continue;
case';': case ';':
break; break;
case IDENTIFIER: case IDENTIFIER:
...@@ -536,7 +506,7 @@ setup() ...@@ -536,7 +506,7 @@ setup()
} }
// put out non-literal terminals // put out non-literal terminals
for i:=TOKSTART; i<=ntokens; i++ { for i := TOKSTART; i <= ntokens; i++ {
// non-literals // non-literals
c := tokset[i].name[0]; c := tokset[i].name[0];
if c != ' ' && c != '$' { if c != ' ' && c != '$' {
...@@ -546,16 +516,16 @@ setup() ...@@ -546,16 +516,16 @@ setup()
// put out names of token names // put out names of token names
fmt.Fprintf(ftable, "var\tToknames\t =[]string {\n"); fmt.Fprintf(ftable, "var\tToknames\t =[]string {\n");
for i:=TOKSTART; i<=ntokens; i++ { for i := TOKSTART; i <= ntokens; i++ {
fmt.Fprintf(ftable, "\t\"%v\",\n", tokset[i].name); fmt.Fprintf(ftable, "\t\"%v\",\n", tokset[i].name);
} }
fmt.Fprintf(ftable, "}\n"); fmt.Fprintf(ftable, "}\n");
// put out names of state names // put out names of state names
fmt.Fprintf(ftable, "var\tStatenames\t =[]string {\n"); fmt.Fprintf(ftable, "var\tStatenames\t =[]string {\n");
// for i:=TOKSTART; i<=ntokens; i++ { // for i:=TOKSTART; i<=ntokens; i++ {
// fmt.Fprintf(ftable, "\t\"%v\",\n", tokset[i].name); // fmt.Fprintf(ftable, "\t\"%v\",\n", tokset[i].name);
// } // }
fmt.Fprintf(ftable, "}\n"); fmt.Fprintf(ftable, "}\n");
fmt.Fprintf(ftable, "\nfunc\n"); fmt.Fprintf(ftable, "\nfunc\n");
...@@ -563,7 +533,7 @@ setup() ...@@ -563,7 +533,7 @@ setup()
fmt.Fprintf(ftable, "switch p {\n"); fmt.Fprintf(ftable, "switch p {\n");
moreprod(); moreprod();
prdptr[0] = []int{NTBASE,start,1,0}; prdptr[0] = []int{NTBASE, start, 1, 0};
nprod = 1; nprod = 1;
curprod := make([]int, RULEINC); curprod := make([]int, RULEINC);
...@@ -590,15 +560,15 @@ setup() ...@@ -590,15 +560,15 @@ setup()
if t == '|' { if t == '|' {
curprod[mem] = prdptr[nprod-1][0]; curprod[mem] = prdptr[nprod-1][0];
mem++; mem++;
} else } else if t == IDENTCOLON {
if t == IDENTCOLON {
curprod[mem] = chfind(1, tokname); curprod[mem] = chfind(1, tokname);
if curprod[mem] < NTBASE { if curprod[mem] < NTBASE {
error("token illegal on LHS of grammar rule"); error("token illegal on LHS of grammar rule");
} }
mem++; mem++;
} else } else {
error("illegal rule: missing semicolon or | ?"); error("illegal rule: missing semicolon or | ?");
}
// read rule body // read rule body
t = gettok(); t = gettok();
...@@ -611,7 +581,7 @@ setup() ...@@ -611,7 +581,7 @@ setup()
mem++; mem++;
if mem >= len(curprod) { if mem >= len(curprod) {
ncurprod := make([]int, mem+RULEINC); ncurprod := make([]int, mem+RULEINC);
for ll:=0; ll<mem; ll++ { for ll := 0; ll < mem; ll++ {
ncurprod[ll] = curprod[ll]; ncurprod[ll] = curprod[ll];
} }
curprod = ncurprod; curprod = ncurprod;
...@@ -624,7 +594,7 @@ setup() ...@@ -624,7 +594,7 @@ setup()
} }
j = chfind(2, tokname); j = chfind(2, tokname);
if j >= NTBASE { if j >= NTBASE {
error("nonterminal "+nontrst[j-NTBASE].name+" illegal after %%prec"); error("nonterminal " + nontrst[j-NTBASE].name + " illegal after %%prec");
} }
levprd[nprod] = toklev[j]; levprd[nprod] = toklev[j];
t = gettok(); t = gettok();
...@@ -640,7 +610,7 @@ setup() ...@@ -640,7 +610,7 @@ setup()
t = gettok(); t = gettok();
if t == IDENTIFIER { if t == IDENTIFIER {
// make it a nonterminal // make it a nonterminal
j = chfind(1, fmt.Sprintf("$$%v",nprod)); j = chfind(1, fmt.Sprintf("$$%v", nprod));
// //
// the current rule will become rule number nprod+1 // the current rule will become rule number nprod+1
...@@ -662,7 +632,7 @@ setup() ...@@ -662,7 +632,7 @@ setup()
mem++; mem++;
if mem >= len(curprod) { if mem >= len(curprod) {
ncurprod := make([]int, mem+RULEINC); ncurprod := make([]int, mem+RULEINC);
for ll:=0; ll<mem; ll++ { for ll := 0; ll < mem; ll++ {
ncurprod[ll] = curprod[ll]; ncurprod[ll] = curprod[ll];
} }
curprod = ncurprod; curprod = ncurprod;
...@@ -678,7 +648,7 @@ setup() ...@@ -678,7 +648,7 @@ setup()
// check that default action is reasonable // check that default action is reasonable
if ntypes != 0 && (levprd[nprod]&ACTFLAG) == 0 && if ntypes != 0 && (levprd[nprod]&ACTFLAG) == 0 &&
nontrst[curprod[0]-NTBASE].value != 0 { nontrst[curprod[0]-NTBASE].value != 0 {
// no explicit action, LHS has value // no explicit action, LHS has value
tempty := curprod[1]; tempty := curprod[1];
if tempty < 0 { if tempty < 0 {
...@@ -686,8 +656,9 @@ setup() ...@@ -686,8 +656,9 @@ setup()
} }
if tempty >= NTBASE { if tempty >= NTBASE {
tempty = nontrst[tempty-NTBASE].value; tempty = nontrst[tempty-NTBASE].value;
} else } else {
tempty = TYPE(toklev[tempty]); tempty = TYPE(toklev[tempty]);
}
if tempty != nontrst[curprod[0]-NTBASE].value { if tempty != nontrst[curprod[0]-NTBASE].value {
error("default action causes potential type clash"); error("default action causes potential type clash");
} }
...@@ -697,7 +668,7 @@ setup() ...@@ -697,7 +668,7 @@ setup()
} }
moreprod(); moreprod();
prdptr[nprod] = make([]int, mem); prdptr[nprod] = make([]int, mem);
for ll:=0; ll<mem; ll++ { for ll := 0; ll < mem; ll++ {
prdptr[nprod][ll] = curprod[ll]; prdptr[nprod][ll] = curprod[ll];
} }
nprod++; nprod++;
...@@ -737,9 +708,7 @@ setup() ...@@ -737,9 +708,7 @@ setup()
// //
// allocate enough room to hold another production // allocate enough room to hold another production
// //
func func moreprod() {
moreprod()
{
n := len(prdptr); n := len(prdptr);
if nprod >= n { if nprod >= n {
nn := n+PRODINC; nn := n+PRODINC;
...@@ -747,7 +716,7 @@ moreprod() ...@@ -747,7 +716,7 @@ moreprod()
alevprd := make([]int, nn); alevprd := make([]int, nn);
arlines := make([]int, nn); arlines := make([]int, nn);
for ll:=0; ll<n; ll++ { for ll := 0; ll < n; ll++ {
aprod[ll] = prdptr[ll]; aprod[ll] = prdptr[ll];
alevprd[ll] = levprd[ll]; alevprd[ll] = levprd[ll];
arlines[ll] = rlines[ll]; arlines[ll] = rlines[ll];
...@@ -763,21 +732,19 @@ moreprod() ...@@ -763,21 +732,19 @@ moreprod()
// define s to be a terminal if t=0 // define s to be a terminal if t=0
// or a nonterminal if t=1 // or a nonterminal if t=1
// //
func func defin(nt int, s string) int {
defin(nt int, s string) int
{
val := 0; val := 0;
if nt != 0 { if nt != 0 {
nnonter++; nnonter++;
if nnonter >= len(nontrst) { if nnonter >= len(nontrst) {
anontrst := make([]Symb, nnonter+SYMINC); anontrst := make([]Symb, nnonter+SYMINC);
for ll:=0; ll<len(nontrst); ll++ { for ll := 0; ll < len(nontrst); ll++ {
anontrst[ll] = nontrst[ll]; anontrst[ll] = nontrst[ll];
} }
nontrst = anontrst; nontrst = anontrst;
} }
nontrst[nnonter] = Symb{s, 0}; nontrst[nnonter] = Symb{s, 0};
return NTBASE + nnonter; return NTBASE+nnonter;
} }
// must be a token // must be a token
...@@ -787,7 +754,7 @@ defin(nt int, s string) int ...@@ -787,7 +754,7 @@ defin(nt int, s string) int
atokset := make([]Symb, nn); atokset := make([]Symb, nn);
atoklev := make([]int, nn); atoklev := make([]int, nn);
for ll:=0; ll<len(tokset); ll++ { for ll := 0; ll < len(tokset); ll++ {
atoklev[ll] = toklev[ll]; atoklev[ll] = toklev[ll];
atokset[ll] = tokset[ll]; atokset[ll] = tokset[ll];
} }
...@@ -802,25 +769,32 @@ defin(nt int, s string) int ...@@ -802,25 +769,32 @@ defin(nt int, s string) int
// single character literal // single character literal
if s[0] == ' ' && len(s) == 1+1 { if s[0] == ' ' && len(s) == 1+1 {
val = int(s[1]); val = int(s[1]);
} else } else if s[0] == ' ' && s[1] == '\\' { // escape sequence
if s[0] == ' ' && s[1] == '\\' { // escape sequence if len(s) == 2+1 {
if(len(s) == 2+1) {
// single character escape sequence // single character escape sequence
switch s[2] { switch s[2] {
case '\'': val = '\''; case '\'':
case '"': val = '"'; val = '\'';
case '\\': val = '\\'; case '"':
case 'a': val = '\a'; val = '"';
case 'b': val = '\b'; case '\\':
case 'n': val = '\n'; val = '\\';
case 'r': val = '\r'; case 'a':
case 't': val = '\t'; val = '\a';
case 'v': val = '\v'; case 'b':
val = '\b';
case 'n':
val = '\n';
case 'r':
val = '\r';
case 't':
val = '\t';
case 'v':
val = '\v';
default: default:
error("invalid escape %v", s[1:3]); error("invalid escape %v", s[1:3]);
} }
} else } else if s[2] == 'u' && len(s) == 2+1+4 { // \unnnn sequence
if s[2] == 'u' && len(s) == 2+1+4 { // \unnnn sequence
val = 0; val = 0;
s = s[3:len(s)]; s = s[3:len(s)];
for s != "" { for s != "" {
...@@ -829,20 +803,21 @@ defin(nt int, s string) int ...@@ -829,20 +803,21 @@ defin(nt int, s string) int
case c >= '0' && c <= '9': case c >= '0' && c <= '9':
c -= '0'; c -= '0';
case c >= 'a' && c <= 'f': case c >= 'a' && c <= 'f':
c -= 'a' - 10; c -= 'a'-10;
case c >= 'A' && c <= 'F': case c >= 'A' && c <= 'F':
c -= 'A' - 10; c -= 'A'-10;
default: default:
error("illegal \\unnnn construction"); error("illegal \\unnnn construction");
} }
val = val * 16 + c; val = val*16 + c;
s = s[1:len(s)]; s = s[1:len(s)];
} }
if val == 0 { if val == 0 {
error("'\\u0000' is illegal"); error("'\\u0000' is illegal");
} }
} else } else {
error("unknown escape"); error("unknown escape");
}
} else { } else {
val = extval; val = extval;
extval++; extval++;
...@@ -852,10 +827,9 @@ defin(nt int, s string) int ...@@ -852,10 +827,9 @@ defin(nt int, s string) int
return ntokens; return ntokens;
} }
var peekline = 0; var peekline = 0
func
gettok() int func gettok() int {
{
var i, match, c int; var i, match, c int;
tokname = ""; tokname = "";
...@@ -903,7 +877,7 @@ gettok() int ...@@ -903,7 +877,7 @@ gettok() int
error("unterminated < ... > clause"); error("unterminated < ... > clause");
} }
for i=1; i<=ntypes; i++ { for i = 1; i <= ntypes; i++ {
if typeset[i] == tokname { if typeset[i] == tokname {
numbval = i; numbval = i;
if tokflag { if tokflag {
...@@ -926,13 +900,12 @@ gettok() int ...@@ -926,13 +900,12 @@ gettok() int
for { for {
c = getrune(finput); c = getrune(finput);
if c == '\n' || c == EOF { if c == '\n' || c == EOF {
error("illegal or missing ' or \"" ); error("illegal or missing ' or \"");
} }
if c == '\\' { if c == '\\' {
tokname += string('\\'); tokname += string('\\');
c = getrune(finput); c = getrune(finput);
} else } else if c == match {
if c == match {
if tokflag { if tokflag {
fmt.Printf(">>> IDENTIFIER \"%v\" %v\n", tokname, lineno); fmt.Printf(">>> IDENTIFIER \"%v\" %v\n", tokname, lineno);
} }
...@@ -963,11 +936,11 @@ gettok() int ...@@ -963,11 +936,11 @@ gettok() int
getword(c); getword(c);
// find a reserved word // find a reserved word
for c=0; c < len(resrv); c++ { for c = 0; c < len(resrv); c++ {
if tokname == resrv[c].name { if tokname == resrv[c].name {
if tokflag { if tokflag {
fmt.Printf(">>> %%%v %v %v\n", tokname, fmt.Printf(">>> %%%v %v %v\n", tokname,
resrv[c].value-PRIVATE, lineno); resrv[c].value - PRIVATE, lineno);
} }
return resrv[c].value; return resrv[c].value;
} }
...@@ -975,13 +948,13 @@ gettok() int ...@@ -975,13 +948,13 @@ gettok() int
error("invalid escape, or illegal reserved word: %v", tokname); error("invalid escape, or illegal reserved word: %v", tokname);
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
numbval = c - '0'; numbval = c-'0';
for { for {
c = getrune(finput); c = getrune(finput);
if !isdigit(c) { if !isdigit(c) {
break; break;
} }
numbval = numbval*10 + c-'0'; numbval = numbval*10 + c - '0';
} }
ungetrune(finput, c); ungetrune(finput, c);
if tokflag { if tokflag {
...@@ -1026,9 +999,7 @@ gettok() int ...@@ -1026,9 +999,7 @@ gettok() int
return IDENTIFIER; return IDENTIFIER;
} }
func func getword(c int) {
getword(c int)
{
tokname = ""; tokname = "";
for isword(c) || isdigit(c) || c == '_' || c == '.' || c == '$' { for isword(c) || isdigit(c) || c == '_' || c == '.' || c == '$' {
tokname += string(c); tokname += string(c);
...@@ -1040,9 +1011,7 @@ getword(c int) ...@@ -1040,9 +1011,7 @@ getword(c int)
// //
// determine the type of a symbol // determine the type of a symbol
// //
func func fdtype(t int) int {
fdtype(t int) int
{
var v int; var v int;
var s string; var s string;
...@@ -1059,18 +1028,16 @@ fdtype(t int) int ...@@ -1059,18 +1028,16 @@ fdtype(t int) int
return v; return v;
} }
func func chfind(t int, s string) int {
chfind(t int, s string) int
{
if s[0] == ' ' { if s[0] == ' ' {
t = 0; t = 0;
} }
for i:=0; i<=ntokens; i++ { for i := 0; i <= ntokens; i++ {
if s == tokset[i].name { if s == tokset[i].name {
return i; return i;
} }
} }
for i:=0; i<=nnonter; i++ { for i := 0; i <= nnonter; i++ {
if s == nontrst[i].name { if s == nontrst[i].name {
return NTBASE+i; return NTBASE+i;
} }
...@@ -1086,9 +1053,7 @@ chfind(t int, s string) int ...@@ -1086,9 +1053,7 @@ chfind(t int, s string) int
// //
// copy the union declaration to the output, and the define file if present // copy the union declaration to the output, and the define file if present
// //
func func cpyunion() {
cpyunion()
{
if !lflag { if !lflag {
fmt.Fprintf(ftable, "\n//line %v %v\n", lineno, infile); fmt.Fprintf(ftable, "\n//line %v %v\n", lineno, infile);
...@@ -1097,14 +1062,14 @@ cpyunion() ...@@ -1097,14 +1062,14 @@ cpyunion()
level := 0; level := 0;
out: out:
for { for {
c := getrune(finput); c := getrune(finput);
if c == EOF { if c == EOF {
error("EOF encountered while processing %%union"); error("EOF encountered while processing %%union");
} }
putrune(ftable, c); putrune(ftable, c);
switch(c) { switch c {
case '\n': case '\n':
lineno++; lineno++;
case '{': case '{':
...@@ -1128,9 +1093,7 @@ cpyunion() ...@@ -1128,9 +1093,7 @@ cpyunion()
// //
// saves code between %{ and %} // saves code between %{ and %}
// //
func func cpycode() {
cpycode()
{
lno := lineno; lno := lineno;
c := getrune(finput); c := getrune(finput);
...@@ -1231,9 +1194,7 @@ cpycode() ...@@ -1231,9 +1194,7 @@ cpycode()
// skip over comments // skip over comments
// skipcom is called after reading a '/' // skipcom is called after reading a '/'
// //
func func skipcom() int {
skipcom() int
{
var c int; var c int;
c = getrune(finput); c = getrune(finput);
...@@ -1254,7 +1215,7 @@ skipcom() int ...@@ -1254,7 +1215,7 @@ skipcom() int
nl := 0; // lines skipped nl := 0; // lines skipped
c = getrune(finput); c = getrune(finput);
l1: l1:
switch c { switch c {
case '*': case '*':
c = getrune(finput); c = getrune(finput);
...@@ -1274,25 +1235,22 @@ skipcom() int ...@@ -1274,25 +1235,22 @@ skipcom() int
return nl; return nl;
} }
func func dumpprod(curprod []int, max int) {
dumpprod(curprod []int, max int)
{
fmt.Printf("\n"); fmt.Printf("\n");
for i:=0; i<max; i++ { for i := 0; i < max; i++ {
p := curprod[i]; p := curprod[i];
if p < 0 { if p < 0 {
fmt.Printf("[%v] %v\n", i, p); fmt.Printf("[%v] %v\n", i, p);
} else } else {
fmt.Printf("[%v] %v\n", i, symnam(p)); fmt.Printf("[%v] %v\n", i, symnam(p));
}
} }
} }
// //
// copy action to the next ; or closing } // copy action to the next ; or closing }
// //
func func cpyact(curprod []int, max int) {
cpyact(curprod []int, max int)
{
if !lflag { if !lflag {
fmt.Fprintf(ftable, "\n//line %v %v\n", lineno, infile); fmt.Fprintf(ftable, "\n//line %v %v\n", lineno, infile);
...@@ -1301,14 +1259,14 @@ cpyact(curprod []int, max int) ...@@ -1301,14 +1259,14 @@ cpyact(curprod []int, max int)
lno := lineno; lno := lineno;
brac := 0; brac := 0;
loop: loop:
for { for {
c := getrune(finput); c := getrune(finput);
swt: swt:
switch c { switch c {
case ';': case ';':
if(brac == 0) { if brac == 0 {
putrune(ftable, c); putrune(ftable, c);
return; return;
} }
...@@ -1316,7 +1274,7 @@ cpyact(curprod []int, max int) ...@@ -1316,7 +1274,7 @@ cpyact(curprod []int, max int)
case '{': case '{':
if brac == 0 { if brac == 0 {
} }
putrune(ftable, '\t'); putrune(ftable, '\t');
brac++; brac++;
case '$': case '$':
...@@ -1352,7 +1310,7 @@ cpyact(curprod []int, max int) ...@@ -1352,7 +1310,7 @@ cpyact(curprod []int, max int)
j := 0; j := 0;
if isdigit(c) { if isdigit(c) {
for isdigit(c) { for isdigit(c) {
j = j*10 + c-'0'; j = j*10 + c - '0';
c = getrune(finput); c = getrune(finput);
} }
ungetrune(finput, c); ungetrune(finput, c);
...@@ -1360,8 +1318,7 @@ cpyact(curprod []int, max int) ...@@ -1360,8 +1318,7 @@ cpyact(curprod []int, max int)
if j >= max { if j >= max {
error("Illegal use of $%v", j); error("Illegal use of $%v", j);
} }
} else } else if isword(c) || c == '_' || c == '.' {
if isword(c) || c == '_' || c == '.' {
// look for $name // look for $name
ungetrune(finput, c); ungetrune(finput, c);
if gettok() != IDENTIFIER { if gettok() != IDENTIFIER {
...@@ -1372,12 +1329,12 @@ cpyact(curprod []int, max int) ...@@ -1372,12 +1329,12 @@ cpyact(curprod []int, max int)
c = getrune(finput); c = getrune(finput);
if c != '@' { if c != '@' {
ungetrune(finput, c); ungetrune(finput, c);
} else } else if gettok() != NUMBER {
if gettok() != NUMBER {
error("@ must be followed by number"); error("@ must be followed by number");
} else } else {
fnd = numbval; fnd = numbval;
for j=1; j<max; j++ { }
for j = 1; j < max; j++ {
if tokn == curprod[j] { if tokn == curprod[j] {
fnd--; fnd--;
if fnd <= 0 { if fnd <= 0 {
...@@ -1444,8 +1401,7 @@ cpyact(curprod []int, max int) ...@@ -1444,8 +1401,7 @@ cpyact(curprod []int, max int)
if c == '\n' { if c == '\n' {
lineno++; lineno++;
} }
} else } else if c == match {
if c == match {
break swt; break swt;
} }
if c == '\n' { if c == '\n' {
...@@ -1468,12 +1424,10 @@ cpyact(curprod []int, max int) ...@@ -1468,12 +1424,10 @@ cpyact(curprod []int, max int)
} }
} }
func func openup() {
openup()
{
infile = flag.Arg(0); infile = flag.Arg(0);
finput = open(infile); finput = open(infile);
if(finput == nil) { if finput == nil {
error("cannot open %v", infile); error("cannot open %v", infile);
} }
...@@ -1499,15 +1453,14 @@ openup() ...@@ -1499,15 +1453,14 @@ openup()
// //
// return a pointer to the name of symbol i // return a pointer to the name of symbol i
// //
func func symnam(i int) string {
symnam(i int) string
{
var s string; var s string;
if i >= NTBASE { if i >= NTBASE {
s = nontrst[i-NTBASE].name; s = nontrst[i-NTBASE].name;
} else } else {
s = tokset[i].name; s = tokset[i].name;
}
if s[0] == ' ' { if s[0] == ' ' {
s = s[1:len(s)]; s = s[1:len(s)];
} }
...@@ -1517,10 +1470,8 @@ symnam(i int) string ...@@ -1517,10 +1470,8 @@ symnam(i int) string
// //
// set elements 0 through n-1 to c // set elements 0 through n-1 to c
// //
func func aryfil(v []int, n, c int) {
aryfil(v []int, n, c int) for i := 0; i < n; i++ {
{
for i:=0; i<n; i++ {
v[i] = c; v[i] = c;
} }
} }
...@@ -1530,26 +1481,24 @@ aryfil(v []int, n, c int) ...@@ -1530,26 +1481,24 @@ aryfil(v []int, n, c int)
// The array pres points to these lists // The array pres points to these lists
// the array pyield has the lists: the total size is only NPROD+1 // the array pyield has the lists: the total size is only NPROD+1
// //
func func cpres() {
cpres()
{
pres = make([][][]int, nnonter+1); pres = make([][][]int, nnonter+1);
curres := make([][]int, nprod); curres := make([][]int, nprod);
if false { if false {
for j:=0; j<=nnonter; j++ { for j := 0; j <= nnonter; j++ {
fmt.Printf("nnonter[%v] = %v\n", j, nontrst[j].name); fmt.Printf("nnonter[%v] = %v\n", j, nontrst[j].name);
} }
for j:=0; j<nprod; j++ { for j := 0; j < nprod; j++ {
fmt.Printf("prdptr[%v][0] = %v+NTBASE\n", j, prdptr[j][0]-NTBASE); fmt.Printf("prdptr[%v][0] = %v+NTBASE\n", j, prdptr[j][0]-NTBASE);
} }
} }
fatfl = 0; // make undefined symbols nonfatal fatfl = 0; // make undefined symbols nonfatal
for i:=0; i<=nnonter; i++ { for i := 0; i <= nnonter; i++ {
n := 0; n := 0;
c := i+NTBASE; c := i+NTBASE;
for j:=0; j<nprod; j++ { for j := 0; j < nprod; j++ {
if prdptr[j][0] == c { if prdptr[j][0] == c {
curres[n] = prdptr[j][1:len(prdptr[j])]; curres[n] = prdptr[j][1:len(prdptr[j])];
n++; n++;
...@@ -1560,7 +1509,7 @@ cpres() ...@@ -1560,7 +1509,7 @@ cpres()
continue; continue;
} }
pres[i] = make([][]int, n); pres[i] = make([][]int, n);
for ll:=0; ll<n; ll++ { for ll := 0; ll < n; ll++ {
pres[i][ll] = curres[ll]; pres[i][ll] = curres[ll];
} }
} }
...@@ -1571,9 +1520,7 @@ cpres() ...@@ -1571,9 +1520,7 @@ cpres()
} }
} }
func func dumppres() {
dumppres()
{
for i := 0; i <= nnonter; i++ { for i := 0; i <= nnonter; i++ {
print("nonterm %d\n", i); print("nonterm %d\n", i);
curres := pres[i]; curres := pres[i];
...@@ -1592,9 +1539,7 @@ dumppres() ...@@ -1592,9 +1539,7 @@ dumppres()
// mark nonterminals which derive the empty string // mark nonterminals which derive the empty string
// also, look for nonterminals which don't derive any token strings // also, look for nonterminals which don't derive any token strings
// //
func func cempty() {
cempty()
{
var i, p, np int; var i, p, np int;
var prd []int; var prd []int;
...@@ -1605,14 +1550,14 @@ cempty() ...@@ -1605,14 +1550,14 @@ cempty()
aryfil(pempty, nnonter+1, WHOKNOWS); aryfil(pempty, nnonter+1, WHOKNOWS);
// now, look at productions, marking nonterminals which derive something // now, look at productions, marking nonterminals which derive something
more: more:
for { for {
for i=0; i<nprod; i++ { for i = 0; i < nprod; i++ {
prd = prdptr[i]; prd = prdptr[i];
if pempty[prd[0] - NTBASE] != 0 { if pempty[prd[0]-NTBASE] != 0 {
continue; continue;
} }
np = len(prd) - 1; np = len(prd)-1;
for p = 1; p < np; p++ { for p = 1; p < np; p++ {
if prd[p] >= NTBASE && pempty[prd[p]-NTBASE] == WHOKNOWS { if prd[p] >= NTBASE && pempty[prd[p]-NTBASE] == WHOKNOWS {
break; break;
...@@ -1628,7 +1573,7 @@ cempty() ...@@ -1628,7 +1573,7 @@ cempty()
} }
// now, look at the nonterminals, to see if they are all OK // now, look at the nonterminals, to see if they are all OK
for i=0; i<=nnonter; i++ { for i = 0; i <= nnonter; i++ {
// the added production rises or falls as the start symbol ... // the added production rises or falls as the start symbol ...
if i == 0 { if i == 0 {
continue; continue;
...@@ -1650,16 +1595,16 @@ cempty() ...@@ -1650,16 +1595,16 @@ cempty()
// loop as long as we keep finding empty nonterminals // loop as long as we keep finding empty nonterminals
again: again:
for { for {
next: next:
for i=1; i<nprod; i++ { for i = 1; i < nprod; i++ {
// not known to be empty // not known to be empty
prd = prdptr[i]; prd = prdptr[i];
if pempty[prd[0]-NTBASE] != WHOKNOWS { if pempty[prd[0]-NTBASE] != WHOKNOWS {
continue; continue;
} }
np = len(prd) - 1; np = len(prd)-1;
for p = 1; p < np; p++ { for p = 1; p < np; p++ {
if prd[p] < NTBASE || pempty[prd[p]-NTBASE] != EMPTY { if prd[p] < NTBASE || pempty[prd[p]-NTBASE] != EMPTY {
continue next; continue next;
...@@ -1676,9 +1621,7 @@ cempty() ...@@ -1676,9 +1621,7 @@ cempty()
} }
} }
func func dumpempty() {
dumpempty()
{
for i := 0; i <= nnonter; i++ { for i := 0; i <= nnonter; i++ {
if pempty[i] == EMPTY { if pempty[i] == EMPTY {
print("non-term %d %s matches empty\n", i, symnam(i+NTBASE)); print("non-term %d %s matches empty\n", i, symnam(i+NTBASE));
...@@ -1689,16 +1632,14 @@ dumpempty() ...@@ -1689,16 +1632,14 @@ dumpempty()
// //
// compute an array with the first of nonterminals // compute an array with the first of nonterminals
// //
func func cpfir() {
cpfir()
{
var s, n, p, np, ch, i int; var s, n, p, np, ch, i int;
var curres [][]int; var curres [][]int;
var prd []int; var prd []int;
wsets = make([]Wset, nnonter+WSETINC); wsets = make([]Wset, nnonter+WSETINC);
pfirst = make([]Lkset, nnonter+1); pfirst = make([]Lkset, nnonter+1);
for i=0; i<=nnonter; i++ { for i = 0; i <= nnonter; i++ {
wsets[i].ws = mkset(); wsets[i].ws = mkset();
pfirst[i] = mkset(); pfirst[i] = mkset();
curres = pres[i]; curres = pres[i];
...@@ -1707,7 +1648,7 @@ cpfir() ...@@ -1707,7 +1648,7 @@ cpfir()
// initially fill the sets // initially fill the sets
for s = 0; s < n; s++ { for s = 0; s < n; s++ {
prd = curres[s]; prd = curres[s];
np = len(prd) - 1; np = len(prd)-1;
for p = 0; p < np; p++ { for p = 0; p < np; p++ {
ch = prd[p]; ch = prd[p];
if ch < NTBASE { if ch < NTBASE {
...@@ -1725,14 +1666,14 @@ cpfir() ...@@ -1725,14 +1666,14 @@ cpfir()
changes := 1; changes := 1;
for changes != 0 { for changes != 0 {
changes = 0; changes = 0;
for i=0; i<=nnonter; i++ { for i = 0; i <= nnonter; i++ {
curres = pres[i]; curres = pres[i];
n = len(curres); n = len(curres);
for s = 0; s < n; s++ { for s = 0; s < n; s++ {
prd = curres[s]; prd = curres[s];
np = len(prd) - 1; np = len(prd)-1;
for p = 0; p < np; p++ { for p = 0; p < np; p++ {
ch = prd[p] - NTBASE; ch = prd[p]-NTBASE;
if ch < 0 { if ch < 0 {
break; break;
} }
...@@ -1749,7 +1690,7 @@ cpfir() ...@@ -1749,7 +1690,7 @@ cpfir()
return; return;
} }
if foutput != nil { if foutput != nil {
for i=0; i<=nnonter; i++ { for i = 0; i <= nnonter; i++ {
fmt.Fprintf(foutput, "\n%v: %v %v\n", fmt.Fprintf(foutput, "\n%v: %v %v\n",
nontrst[i].name, pfirst[i], pempty[i]); nontrst[i].name, pfirst[i], pempty[i]);
} }
...@@ -1759,9 +1700,7 @@ cpfir() ...@@ -1759,9 +1700,7 @@ cpfir()
// //
// generate the states // generate the states
// //
func func stagen() {
stagen()
{
// initialize // initialize
nstate = 0; nstate = 0;
tstates = make([]int, ntokens+1); // states generated by terminal gotos tstates = make([]int, ntokens+1); // states generated by terminal gotos
...@@ -1788,7 +1727,7 @@ stagen() ...@@ -1788,7 +1727,7 @@ stagen()
first := 1; first := 1;
for more := 1; more != 0; first = 0 { for more := 1; more != 0; first = 0 {
more = 0; more = 0;
for i:=0; i<nstate; i++ { for i := 0; i < nstate; i++ {
if tystate[i] != MUSTDO { if tystate[i] != MUSTDO {
continue; continue;
} }
...@@ -1800,7 +1739,7 @@ stagen() ...@@ -1800,7 +1739,7 @@ stagen()
closure(i); closure(i);
// generate goto's // generate goto's
for p:=0; p<cwp; p++ { for p := 0; p < cwp; p++ {
pi := wsets[p]; pi := wsets[p];
if pi.flag != 0 { if pi.flag != 0 {
continue; continue;
...@@ -1816,7 +1755,7 @@ stagen() ...@@ -1816,7 +1755,7 @@ stagen()
// do a goto on c // do a goto on c
putitem(wsets[p].pitem, wsets[p].ws); putitem(wsets[p].pitem, wsets[p].ws);
for q:=p+1; q<cwp; q++ { for q := p+1; q < cwp; q++ {
// this item contributes to the goto // this item contributes to the goto
if c == wsets[q].pitem.first { if c == wsets[q].pitem.first {
putitem(wsets[q].pitem, wsets[q].ws); putitem(wsets[q].pitem, wsets[q].ws);
...@@ -1826,13 +1765,14 @@ stagen() ...@@ -1826,13 +1765,14 @@ stagen()
if c < NTBASE { if c < NTBASE {
state(c); // register new state state(c); // register new state
} else } else {
temp1[c-NTBASE] = state(c); temp1[c-NTBASE] = state(c);
}
} }
if gsdebug != 0 && foutput != nil { if gsdebug != 0 && foutput != nil {
fmt.Fprintf(foutput, "%v: ", i); fmt.Fprintf(foutput, "%v: ", i);
for j:=0; j<=nnonter; j++ { for j := 0; j <= nnonter; j++ {
if temp1[j] != 0 { if temp1[j] != 0 {
fmt.Fprintf(foutput, "%v %v,", nontrst[j].name, temp1[j]); fmt.Fprintf(foutput, "%v %v,", nontrst[j].name, temp1[j]);
} }
...@@ -1852,18 +1792,16 @@ stagen() ...@@ -1852,18 +1792,16 @@ stagen()
// //
// generate the closure of state i // generate the closure of state i
// //
func func closure(i int) {
closure(i int)
{
zzclose++; zzclose++;
// first, copy kernel of state i to wsets // first, copy kernel of state i to wsets
cwp = 0; cwp = 0;
q := pstate[i+1]; q := pstate[i+1];
for p:=pstate[i]; p<q; p++ { for p := pstate[i]; p < q; p++ {
wsets[cwp].pitem = statemem[p].pitem; wsets[cwp].pitem = statemem[p].pitem;
wsets[cwp].flag = 1; // this item must get closed wsets[cwp].flag = 1; // this item must get closed
for ll:=0; ll<len(wsets[cwp].ws); ll++ { for ll := 0; ll < len(wsets[cwp].ws); ll++ {
wsets[cwp].ws[ll] = statemem[p].look[ll]; wsets[cwp].ws[ll] = statemem[p].look[ll];
} }
cwp++; cwp++;
...@@ -1873,7 +1811,7 @@ closure(i int) ...@@ -1873,7 +1811,7 @@ closure(i int)
work := 1; work := 1;
for work != 0 { for work != 0 {
work = 0; work = 0;
for u:=0; u<cwp; u++ { for u := 0; u < cwp; u++ {
if wsets[u].flag == 0 { if wsets[u].flag == 0 {
continue; continue;
} }
...@@ -1890,7 +1828,7 @@ closure(i int) ...@@ -1890,7 +1828,7 @@ closure(i int)
aryfil(clset, tbitset, 0); aryfil(clset, tbitset, 0);
// find items involving c // find items involving c
for v:=u; v<cwp; v++ { for v := u; v < cwp; v++ {
if wsets[v].flag != 1 || wsets[v].pitem.first != c { if wsets[v].flag != 1 || wsets[v].pitem.first != c {
continue; continue;
} }
...@@ -1930,7 +1868,7 @@ closure(i int) ...@@ -1930,7 +1868,7 @@ closure(i int)
curres := pres[c-NTBASE]; curres := pres[c-NTBASE];
n := len(curres); n := len(curres);
nexts: nexts:
// initially fill the sets // initially fill the sets
for s := 0; s < n; s++ { for s := 0; s < n; s++ {
prd := curres[s]; prd := curres[s];
...@@ -1939,12 +1877,12 @@ closure(i int) ...@@ -1939,12 +1877,12 @@ closure(i int)
// put these items into the closure // put these items into the closure
// is the item there // is the item there
// //
for v:=0; v<cwp; v++ { for v := 0; v < cwp; v++ {
// yes, it is there // yes, it is there
if wsets[v].pitem.off == 0 && if wsets[v].pitem.off == 0 &&
aryeq(wsets[v].pitem.prod, prd) != 0 { aryeq(wsets[v].pitem.prod, prd) != 0 {
if nolook == 0 && if nolook == 0 &&
setunion(wsets[v].ws, clset) != 0 { setunion(wsets[v].ws, clset) != 0 {
wsets[v].flag = 1; wsets[v].flag = 1;
work = 1; work = 1;
} }
...@@ -1955,7 +1893,7 @@ closure(i int) ...@@ -1955,7 +1893,7 @@ closure(i int)
// not there; make a new entry // not there; make a new entry
if cwp >= len(wsets) { if cwp >= len(wsets) {
awsets := make([]Wset, cwp+WSETINC); awsets := make([]Wset, cwp+WSETINC);
for ll:=0; ll<len(wsets); ll++ { for ll := 0; ll < len(wsets); ll++ {
awsets[ll] = wsets[ll]; awsets[ll] = wsets[ll];
} }
wsets = awsets; wsets = awsets;
...@@ -1965,7 +1903,7 @@ closure(i int) ...@@ -1965,7 +1903,7 @@ closure(i int)
wsets[cwp].ws = mkset(); wsets[cwp].ws = mkset();
if nolook == 0 { if nolook == 0 {
work = 1; work = 1;
for ll:=0; ll<len(wsets[cwp].ws); ll++ { for ll := 0; ll < len(wsets[cwp].ws); ll++ {
wsets[cwp].ws[ll] = clset[ll]; wsets[cwp].ws[ll] = clset[ll];
} }
} }
...@@ -1977,7 +1915,7 @@ closure(i int) ...@@ -1977,7 +1915,7 @@ closure(i int)
// have computed closure; flags are reset; return // have computed closure; flags are reset; return
if cldebug != 0 && foutput != nil { if cldebug != 0 && foutput != nil {
fmt.Fprintf(foutput, "\nState %v, nolook = %v\n", i, nolook); fmt.Fprintf(foutput, "\nState %v, nolook = %v\n", i, nolook);
for u:=0; u<cwp; u++ { for u := 0; u < cwp; u++ {
if wsets[u].flag != 0 { if wsets[u].flag != 0 {
fmt.Fprintf(foutput, "flag set\n"); fmt.Fprintf(foutput, "flag set\n");
} }
...@@ -1992,9 +1930,7 @@ closure(i int) ...@@ -1992,9 +1930,7 @@ closure(i int)
// //
// sorts last state,and sees if it equals earlier ones. returns state number // sorts last state,and sees if it equals earlier ones. returns state number
// //
func func state(c int) int {
state(c int) int
{
zzstate++; zzstate++;
p1 := pstate[nstate]; p1 := pstate[nstate];
p2 := pstate[nstate+1]; p2 := pstate[nstate+1];
...@@ -2006,38 +1942,40 @@ state(c int) int ...@@ -2006,38 +1942,40 @@ state(c int) int
var k, l int; var k, l int;
for k = p1+1; k < p2; k++ { // make k the biggest for k = p1+1; k < p2; k++ { // make k the biggest
for l = k; l > p1; l-- { for l = k; l > p1; l-- {
if(statemem[l].pitem.prodno < statemem[l-1].pitem.prodno || if statemem[l].pitem.prodno < statemem[l-1].pitem.prodno ||
statemem[l].pitem.prodno == statemem[l-1].pitem.prodno && statemem[l].pitem.prodno == statemem[l-1].pitem.prodno &&
statemem[l].pitem.off < statemem[l-1].pitem.off) { statemem[l].pitem.off < statemem[l-1].pitem.off {
s := statemem[l]; s := statemem[l];
statemem[l] = statemem[l-1]; statemem[l] = statemem[l-1];
statemem[l-1] = s; statemem[l-1] = s;
} else } else {
break; break;
}
} }
} }
size1 := p2 - p1; // size of state size1 := p2-p1; // size of state
var i int; var i int;
if c >= NTBASE { if c >= NTBASE {
i = ntstates[c-NTBASE]; i = ntstates[c-NTBASE];
} else } else {
i = tstates[c]; i = tstates[c];
}
look: look:
for ; i != 0; i = mstates[i] { for ; i != 0; i = mstates[i] {
// get ith state // get ith state
q1 := pstate[i]; q1 := pstate[i];
q2 := pstate[i+1]; q2 := pstate[i+1];
size2 := q2 - q1; size2 := q2-q1;
if size1 != size2 { if size1 != size2 {
continue; continue;
} }
k = p1; k = p1;
for l = q1; l < q2; l++ { for l = q1; l < q2; l++ {
if aryeq(statemem[l].pitem.prod, statemem[k].pitem.prod) == 0 || if aryeq(statemem[l].pitem.prod, statemem[k].pitem.prod) == 0 ||
statemem[l].pitem.off != statemem[k].pitem.off { statemem[l].pitem.off != statemem[k].pitem.off {
continue look; continue look;
} }
k++; k++;
...@@ -2081,9 +2019,7 @@ state(c int) int ...@@ -2081,9 +2019,7 @@ state(c int) int
return nstate-1; return nstate-1;
} }
func func putitem(p Pitem, set Lkset) {
putitem(p Pitem, set Lkset)
{
p.off++; p.off++;
p.first = p.prod[p.off]; p.first = p.prod[p.off];
...@@ -2093,7 +2029,7 @@ putitem(p Pitem, set Lkset) ...@@ -2093,7 +2029,7 @@ putitem(p Pitem, set Lkset)
j := pstate[nstate+1]; j := pstate[nstate+1];
if j >= len(statemem) { if j >= len(statemem) {
asm := make([]Item, j+STATEINC); asm := make([]Item, j+STATEINC);
for ll:=0; ll<len(statemem); ll++ { for ll := 0; ll < len(statemem); ll++ {
asm[ll] = statemem[ll]; asm[ll] = statemem[ll];
} }
statemem = asm; statemem = asm;
...@@ -2101,7 +2037,7 @@ putitem(p Pitem, set Lkset) ...@@ -2101,7 +2037,7 @@ putitem(p Pitem, set Lkset)
statemem[j].pitem = p; statemem[j].pitem = p;
if nolook == 0 { if nolook == 0 {
s := mkset(); s := mkset();
for ll:=0; ll<len(set); ll++ { for ll := 0; ll < len(set); ll++ {
s[ll] = set[ll]; s[ll] = set[ll];
} }
statemem[j].look = s; statemem[j].look = s;
...@@ -2113,13 +2049,11 @@ putitem(p Pitem, set Lkset) ...@@ -2113,13 +2049,11 @@ putitem(p Pitem, set Lkset)
// //
// creates output string for item pointed to by pp // creates output string for item pointed to by pp
// //
func func writem(pp Pitem) string {
writem(pp Pitem) string
{
var i int; var i int;
p := pp.prod; p := pp.prod;
q := chcopy(nontrst[prdptr[pp.prodno][0]-NTBASE].name) + ": "; q := chcopy(nontrst[prdptr[pp.prodno][0]-NTBASE].name)+": ";
npi := pp.off; npi := pp.off;
pi := aryeq(p, prdptr[pp.prodno]); pi := aryeq(p, prdptr[pp.prodno]);
...@@ -2151,9 +2085,7 @@ writem(pp Pitem) string ...@@ -2151,9 +2085,7 @@ writem(pp Pitem) string
// //
// pack state i from temp1 into amem // pack state i from temp1 into amem
// //
func func apack(p []int, n int) int {
apack(p []int, n int) int
{
// //
// we don't need to worry about checking because // we don't need to worry about checking because
// we will only look at entries known to be there... // we will only look at entries known to be there...
...@@ -2165,18 +2097,18 @@ apack(p []int, n int) int ...@@ -2165,18 +2097,18 @@ apack(p []int, n int) int
off--; off--;
} }
// no actions // no actions
if pp > n { if pp > n {
return 0; return 0;
} }
for ; n > pp && p[n] == 0; n-- { for ; n > pp && p[n] == 0; n-- {
} }
p = p[pp:n+1]; p = p[pp : n+1];
// now, find a place for the elements from p to q, inclusive // now, find a place for the elements from p to q, inclusive
r := len(amem) - len(p); r := len(amem)-len(p);
nextk: nextk:
for rr := 0; rr <= r; rr++ { for rr := 0; rr <= r; rr++ {
qq := rr; qq := rr;
for pp = 0; pp < len(p); pp++ { for pp = 0; pp < len(p); pp++ {
...@@ -2211,7 +2143,7 @@ apack(p []int, n int) int ...@@ -2211,7 +2143,7 @@ apack(p []int, n int) int
fmt.Fprintf(foutput, "\n"); fmt.Fprintf(foutput, "\n");
} }
} }
return off + rr; return off+rr;
} }
error("no space in action table"); error("no space in action table");
return 0; return 0;
...@@ -2220,9 +2152,7 @@ apack(p []int, n int) int ...@@ -2220,9 +2152,7 @@ apack(p []int, n int) int
// //
// print the output for the states // print the output for the states
// //
func func output() {
output()
{
var c, u, v int; var c, u, v int;
fmt.Fprintf(ftable, "var\tYYEXCA = []int {\n"); fmt.Fprintf(ftable, "var\tYYEXCA = []int {\n");
...@@ -2230,7 +2160,7 @@ output() ...@@ -2230,7 +2160,7 @@ output()
noset := mkset(); noset := mkset();
// output the stuff for state i // output the stuff for state i
for i:=0; i<nstate; i++ { for i := 0; i < nstate; i++ {
nolook = 0; nolook = 0;
if tystate[i] != MUSTLOOKAHEAD { if tystate[i] != MUSTLOOKAHEAD {
nolook = 1; nolook = 1;
...@@ -2240,19 +2170,18 @@ output() ...@@ -2240,19 +2170,18 @@ output()
// output actions // output actions
nolook = 1; nolook = 1;
aryfil(temp1, ntokens+nnonter+1, 0); aryfil(temp1, ntokens+nnonter+1, 0);
for u=0; u<cwp; u++ { for u = 0; u < cwp; u++ {
c = wsets[u].pitem.first; c = wsets[u].pitem.first;
if c > 1 && c < NTBASE && temp1[c] == 0 { if c > 1 && c < NTBASE && temp1[c] == 0 {
for v=u; v<cwp; v++ { for v = u; v < cwp; v++ {
if c == wsets[v].pitem.first { if c == wsets[v].pitem.first {
putitem(wsets[v].pitem, noset); putitem(wsets[v].pitem, noset);
} }
} }
temp1[c] = state(c); temp1[c] = state(c);
} else } else if c > NTBASE {
if c > NTBASE {
c -= NTBASE; c -= NTBASE;
if temp1[c + ntokens] == 0 { if temp1[c+ntokens] == 0 {
temp1[c+ntokens] = amem[indgo[i]+c]; temp1[c+ntokens] = amem[indgo[i]+c];
} }
} }
...@@ -2263,7 +2192,7 @@ output() ...@@ -2263,7 +2192,7 @@ output()
// now, we have the shifts; look at the reductions // now, we have the shifts; look at the reductions
lastred = 0; lastred = 0;
for u=0; u<cwp; u++ { for u = 0; u < cwp; u++ {
c = wsets[u].pitem.first; c = wsets[u].pitem.first;
// reduction // reduction
...@@ -2272,27 +2201,27 @@ output() ...@@ -2272,27 +2201,27 @@ output()
} }
lastred = -c; lastred = -c;
us := wsets[u].ws; us := wsets[u].ws;
for k:=0; k<=ntokens; k++ { for k := 0; k <= ntokens; k++ {
if bitset(us, k) == 0 { if bitset(us, k) == 0 {
continue; continue;
} }
if temp1[k] == 0 { if temp1[k] == 0 {
temp1[k] = c; temp1[k] = c;
} else } else if temp1[k] < 0 { // reduce/reduce conflict
if temp1[k] < 0 { // reduce/reduce conflict
if foutput != nil { if foutput != nil {
fmt.Fprintf(foutput, fmt.Fprintf(foutput,
"\n %v: reduce/reduce conflict (red'ns " "\n %v: reduce/reduce conflict (red'ns "
"%v and %v) on %v", "%v and %v) on %v",
i, -temp1[k], lastred, symnam(k)); i, -temp1[k], lastred, symnam(k));
} }
if -temp1[k] > lastred { if -temp1[k] > lastred {
temp1[k] = -lastred; temp1[k] = -lastred;
} }
zzrrconf++; zzrrconf++;
} else } else {
// potential shift/reduce conflict // potential shift/reduce conflict
precftn(lastred, k, i); precftn(lastred, k, i);
}
} }
} }
wract(i); wract(i);
...@@ -2311,9 +2240,7 @@ output() ...@@ -2311,9 +2240,7 @@ output()
// the conflict is in state s // the conflict is in state s
// temp1[t] is changed to reflect the action // temp1[t] is changed to reflect the action
// //
func func precftn(r, t, s int) {
precftn(r, t, s int)
{
var action int; var action int;
lp := levprd[r]; lp := levprd[r];
...@@ -2330,15 +2257,15 @@ precftn(r, t, s int) ...@@ -2330,15 +2257,15 @@ precftn(r, t, s int)
} }
if PLEVEL(lt) == PLEVEL(lp) { if PLEVEL(lt) == PLEVEL(lp) {
action = ASSOC(lt); action = ASSOC(lt);
} else } else if PLEVEL(lt) > PLEVEL(lp) {
if PLEVEL(lt) > PLEVEL(lp) { action = RASC; // shift
action = RASC; // shift } else {
} else action = LASC;
action = LASC; // reduce } // reduce
switch action { switch action {
case BASC: // error action case BASC: // error action
temp1[t] = ERRCODE; temp1[t] = ERRCODE;
case LASC: // reduce case LASC: // reduce
temp1[t] = -r; temp1[t] = -r;
} }
} }
...@@ -2347,15 +2274,13 @@ precftn(r, t, s int) ...@@ -2347,15 +2274,13 @@ precftn(r, t, s int)
// output state i // output state i
// temp1 has the actions, lastred the default // temp1 has the actions, lastred the default
// //
func func wract(i int) {
wract(i int)
{
var p, p1 int; var p, p1 int;
// find the best choice for lastred // find the best choice for lastred
lastred = 0; lastred = 0;
ntimes := 0; ntimes := 0;
for j:=0; j<=ntokens; j++ { for j := 0; j <= ntokens; j++ {
if temp1[j] >= 0 { if temp1[j] >= 0 {
continue; continue;
} }
...@@ -2366,7 +2291,7 @@ wract(i int) ...@@ -2366,7 +2291,7 @@ wract(i int)
count := 0; count := 0;
tred := -temp1[j]; tred := -temp1[j];
levprd[tred] |= REDFLAG; levprd[tred] |= REDFLAG;
for p=0; p<=ntokens; p++ { for p = 0; p <= ntokens; p++ {
if temp1[p]+tred == 0 { if temp1[p]+tred == 0 {
count++; count++;
} }
...@@ -2388,7 +2313,7 @@ wract(i int) ...@@ -2388,7 +2313,7 @@ wract(i int)
// clear out entries in temp1 which equal lastred // clear out entries in temp1 which equal lastred
// count entries in optst table // count entries in optst table
n := 0; n := 0;
for p=0; p<=ntokens; p++ { for p = 0; p <= ntokens; p++ {
p1 = temp1[p]; p1 = temp1[p];
if p1+lastred == 0 { if p1+lastred == 0 {
temp1[p] = 0; temp1[p] = 0;
...@@ -2404,16 +2329,14 @@ wract(i int) ...@@ -2404,16 +2329,14 @@ wract(i int)
flag := 0; flag := 0;
os := make([]int, n*2); os := make([]int, n*2);
n = 0; n = 0;
for p=0; p<=ntokens; p++ { for p = 0; p <= ntokens; p++ {
p1 = temp1[p]; p1 = temp1[p];
if p1 != 0 { if p1 != 0 {
if p1 < 0 { if p1 < 0 {
p1 = -p1; p1 = -p1;
} else } else if p1 == ACCEPTCODE {
if p1 == ACCEPTCODE {
p1 = -1; p1 = -1;
} else } else if p1 == ERRCODE {
if p1 == ERRCODE {
p1 = 0; p1 = 0;
} else { } else {
os[n] = p; os[n] = p;
...@@ -2441,9 +2364,7 @@ wract(i int) ...@@ -2441,9 +2364,7 @@ wract(i int)
// //
// writes state i // writes state i
// //
func func wrstate(i int) {
wrstate(i int)
{
var j0, j1, u int; var j0, j1, u int;
var pp, qq int; var pp, qq int;
...@@ -2452,12 +2373,12 @@ wrstate(i int) ...@@ -2452,12 +2373,12 @@ wrstate(i int)
} }
fmt.Fprintf(foutput, "\nstate %v\n", i); fmt.Fprintf(foutput, "\nstate %v\n", i);
qq = pstate[i+1]; qq = pstate[i+1];
for pp=pstate[i]; pp<qq; pp++ { for pp = pstate[i]; pp < qq; pp++ {
fmt.Fprintf(foutput, "\t%v\n", writem(statemem[pp].pitem)); fmt.Fprintf(foutput, "\t%v\n", writem(statemem[pp].pitem));
} }
if tystate[i] == MUSTLOOKAHEAD { if tystate[i] == MUSTLOOKAHEAD {
// print out empty productions in closure // print out empty productions in closure
for u = pstate[i+1] - pstate[i]; u < cwp; u++ { for u = pstate[i+1]-pstate[i]; u < cwp; u++ {
if wsets[u].pitem.first < 0 { if wsets[u].pitem.first < 0 {
fmt.Fprintf(foutput, "\t%v\n", writem(wsets[u].pitem)); fmt.Fprintf(foutput, "\t%v\n", writem(wsets[u].pitem));
} }
...@@ -2465,7 +2386,7 @@ wrstate(i int) ...@@ -2465,7 +2386,7 @@ wrstate(i int)
} }
// check for state equal to another // check for state equal to another
for j0=0; j0<=ntokens; j0++ { for j0 = 0; j0 <= ntokens; j0++ {
j1 = temp1[j0]; j1 = temp1[j0];
if j1 != 0 { if j1 != 0 {
fmt.Fprintf(foutput, "\n\t%v ", symnam(j0)); fmt.Fprintf(foutput, "\n\t%v ", symnam(j0));
...@@ -2474,13 +2395,14 @@ wrstate(i int) ...@@ -2474,13 +2395,14 @@ wrstate(i int)
if j1 > 0 { if j1 > 0 {
if j1 == ACCEPTCODE { if j1 == ACCEPTCODE {
fmt.Fprintf(foutput, "accept"); fmt.Fprintf(foutput, "accept");
} else } else if j1 == ERRCODE {
if j1 == ERRCODE {
fmt.Fprintf(foutput, "error"); fmt.Fprintf(foutput, "error");
} else } else {
fmt.Fprintf(foutput, "shift %v", j1); fmt.Fprintf(foutput, "shift %v", j1);
} else }
} else {
fmt.Fprintf(foutput, "reduce %v (src line %v)", -j1, rlines[-j1]); fmt.Fprintf(foutput, "reduce %v (src line %v)", -j1, rlines[-j1]);
}
} }
} }
...@@ -2488,8 +2410,9 @@ wrstate(i int) ...@@ -2488,8 +2410,9 @@ wrstate(i int)
if lastred != 0 { if lastred != 0 {
fmt.Fprintf(foutput, "\n\t. reduce %v (src line %v)\n\n", fmt.Fprintf(foutput, "\n\t. reduce %v (src line %v)\n\n",
lastred, rlines[lastred]); lastred, rlines[lastred]);
} else } else {
fmt.Fprintf(foutput, "\n\t. error\n\n"); fmt.Fprintf(foutput, "\n\t. error\n\n");
}
// now, output nonterminal actions // now, output nonterminal actions
j1 = ntokens; j1 = ntokens;
...@@ -2504,9 +2427,7 @@ wrstate(i int) ...@@ -2504,9 +2427,7 @@ wrstate(i int)
// //
// output the gotos for the nontermninals // output the gotos for the nontermninals
// //
func func go2out() {
go2out()
{
for i := 1; i <= nnonter; i++ { for i := 1; i <= nnonter; i++ {
go2gen(i); go2gen(i);
...@@ -2545,7 +2466,7 @@ go2out() ...@@ -2545,7 +2466,7 @@ go2out()
n++; n++;
} }
} }
goent := make([]int, 2*n+1); goent := make([]int, 2*n + 1);
n = 0; n = 0;
for j := 0; j < nstate; j++ { for j := 0; j < nstate; j++ {
if tystate[j] != 0 && tystate[j] != best { if tystate[j] != 0 && tystate[j] != best {
...@@ -2571,9 +2492,7 @@ go2out() ...@@ -2571,9 +2492,7 @@ go2out()
// //
// output the gotos for nonterminal c // output the gotos for nonterminal c
// //
func func go2gen(c int) {
go2gen(c int)
{
var i, cc, p, q int; var i, cc, p, q int;
// first, find nonterminals with gotos on c // first, find nonterminals with gotos on c
...@@ -2582,15 +2501,15 @@ go2gen(c int) ...@@ -2582,15 +2501,15 @@ go2gen(c int)
work := 1; work := 1;
for work != 0 { for work != 0 {
work = 0; work = 0;
for i=0; i<nprod; i++ { for i = 0; i < nprod; i++ {
// cc is a nonterminal with a goto on c // cc is a nonterminal with a goto on c
cc = prdptr[i][1]-NTBASE; cc = prdptr[i][1]-NTBASE;
if cc >= 0 && temp1[cc] != 0 { if cc >= 0 && temp1[cc] != 0 {
// thus, the left side of production i does too // thus, the left side of production i does too
cc = prdptr[i][0]-NTBASE; cc = prdptr[i][0]-NTBASE;
if temp1[cc] == 0 { if temp1[cc] == 0 {
work = 1; work = 1;
temp1[cc] = 1; temp1[cc] = 1;
} }
} }
} }
...@@ -2599,7 +2518,7 @@ go2gen(c int) ...@@ -2599,7 +2518,7 @@ go2gen(c int)
// now, we have temp1[c] = 1 if a goto on c in closure of cc // now, we have temp1[c] = 1 if a goto on c in closure of cc
if g2debug != 0 && foutput != nil { if g2debug != 0 && foutput != nil {
fmt.Fprintf(foutput, "%v: gotos on ", nontrst[c].name); fmt.Fprintf(foutput, "%v: gotos on ", nontrst[c].name);
for i=0; i<=nnonter; i++ { for i = 0; i <= nnonter; i++ {
if temp1[i] != 0 { if temp1[i] != 0 {
fmt.Fprintf(foutput, "%v ", nontrst[i].name); fmt.Fprintf(foutput, "%v ", nontrst[i].name);
} }
...@@ -2609,9 +2528,9 @@ go2gen(c int) ...@@ -2609,9 +2528,9 @@ go2gen(c int)
// now, go through and put gotos into tystate // now, go through and put gotos into tystate
aryfil(tystate, nstate, 0); aryfil(tystate, nstate, 0);
for i=0; i<nstate; i++ { for i = 0; i < nstate; i++ {
q = pstate[i+1]; q = pstate[i+1];
for p=pstate[i]; p<q; p++ { for p = pstate[i]; p < q; p++ {
cc = statemem[p].pitem.first; cc = statemem[p].pitem.first;
if cc >= NTBASE { if cc >= NTBASE {
// goto on c is possible // goto on c is possible
...@@ -2630,13 +2549,11 @@ go2gen(c int) ...@@ -2630,13 +2549,11 @@ go2gen(c int)
// the action array is known, we hide the nonterminals // the action array is known, we hide the nonterminals
// derived by productions in levprd. // derived by productions in levprd.
// //
func func hideprod() {
hideprod()
{
nred := 0; nred := 0;
levprd[0] = 0; levprd[0] = 0;
for i:=1; i<nprod; i++ { for i := 1; i < nprod; i++ {
if (levprd[i] & REDFLAG) == 0 { if (levprd[i]&REDFLAG) == 0 {
if foutput != nil { if foutput != nil {
fmt.Fprintf(foutput, "Rule not reduced: %v\n", fmt.Fprintf(foutput, "Rule not reduced: %v\n",
writem(Pitem{prdptr[i], 0, 0, i})); writem(Pitem{prdptr[i], 0, 0, i}));
...@@ -2644,16 +2561,14 @@ hideprod() ...@@ -2644,16 +2561,14 @@ hideprod()
fmt.Printf("rule %v never reduced\n", writem(Pitem{prdptr[i], 0, 0, i})); fmt.Printf("rule %v never reduced\n", writem(Pitem{prdptr[i], 0, 0, i}));
nred++; nred++;
} }
levprd[i] = prdptr[i][0] - NTBASE; levprd[i] = prdptr[i][0]-NTBASE;
} }
if nred != 0 { if nred != 0 {
fmt.Printf("%v rules never reduced\n", nred); fmt.Printf("%v rules never reduced\n", nred);
} }
} }
func func callopt() {
callopt()
{
var j, k, p, q, i int; var j, k, p, q, i int;
var v []int; var v []int;
...@@ -2678,7 +2593,7 @@ callopt() ...@@ -2678,7 +2593,7 @@ callopt()
// nontrivial situation // nontrivial situation
if k <= j { if k <= j {
// j is now the range // j is now the range
// j -= k; // call scj // j -= k; // call scj
if k > maxoff { if k > maxoff {
maxoff = k; maxoff = k;
} }
...@@ -2697,8 +2612,8 @@ callopt() ...@@ -2697,8 +2612,8 @@ callopt()
// minimum entry index is always 0 // minimum entry index is always 0
v = yypgo[i]; v = yypgo[i];
q = len(v) - 1; q = len(v)-1;
for p = 0; p < q ; p += 2 { for p = 0; p < q; p += 2 {
ggreed[i] += 2; ggreed[i] += 2;
if v[p] > j { if v[p] > j {
j = v[p]; j = v[p];
...@@ -2726,8 +2641,9 @@ callopt() ...@@ -2726,8 +2641,9 @@ callopt()
for i != NOMORE { for i != NOMORE {
if i >= 0 { if i >= 0 {
stin(i); stin(i);
} else } else {
gin(-i); gin(-i);
}
i = nxti(); i = nxti();
} }
...@@ -2749,9 +2665,7 @@ callopt() ...@@ -2749,9 +2665,7 @@ callopt()
// //
// finds the next i // finds the next i
// //
func func nxti() int {
nxti() int
{
max := 0; max := 0;
maxi := 0; maxi := 0;
for i := 1; i <= nnonter; i++ { for i := 1; i <= nnonter; i++ {
...@@ -2772,25 +2686,23 @@ nxti() int ...@@ -2772,25 +2686,23 @@ nxti() int
return maxi; return maxi;
} }
func func gin(i int) {
gin(i int)
{
var s int; var s int;
// enter gotos on nonterminal i into array amem // enter gotos on nonterminal i into array amem
ggreed[i] = 0; ggreed[i] = 0;
q := yypgo[i]; q := yypgo[i];
nq := len(q) - 1; nq := len(q)-1;
// now, find amem place for it // now, find amem place for it
nextgp: nextgp:
for p := 0; p < ACTSIZE; p++ { for p := 0; p < ACTSIZE; p++ {
if amem[p] != 0 { if amem[p] != 0 {
continue; continue;
} }
for r := 0; r < nq; r += 2 { for r := 0; r < nq; r += 2 {
s = p + q[r] + 1; s = p+q[r]+1;
if s > maxa { if s > maxa {
maxa = s; maxa = s;
if maxa >= ACTSIZE { if maxa >= ACTSIZE {
...@@ -2808,7 +2720,7 @@ gin(i int) ...@@ -2808,7 +2720,7 @@ gin(i int)
maxa = p; maxa = p;
} }
for r := 0; r < nq; r += 2 { for r := 0; r < nq; r += 2 {
s = p + q[r] + 1; s = p+q[r]+1;
amem[s] = q[r+1]; amem[s] = q[r+1];
} }
pgo[i] = p; pgo[i] = p;
...@@ -2820,9 +2732,7 @@ gin(i int) ...@@ -2820,9 +2732,7 @@ gin(i int)
error("cannot place goto %v\n", i); error("cannot place goto %v\n", i);
} }
func func stin(i int) {
stin(i int)
{
var s int; var s int;
tystate[i] = 0; tystate[i] = 0;
...@@ -2831,25 +2741,24 @@ stin(i int) ...@@ -2831,25 +2741,24 @@ stin(i int)
q := optst[i]; q := optst[i];
nq := len(q); nq := len(q);
nextn: nextn:
// find an acceptable place // find an acceptable place
for n := -maxoff; n < ACTSIZE; n++ { for n := -maxoff; n < ACTSIZE; n++ {
flag := 0; flag := 0;
for r := 0; r < nq; r += 2 { for r := 0; r < nq; r += 2 {
s = q[r] + n; s = q[r]+n;
if s < 0 || s > ACTSIZE { if s < 0 || s > ACTSIZE {
continue nextn; continue nextn;
} }
if amem[s] == 0 { if amem[s] == 0 {
flag++; flag++;
} else } else if amem[s] != q[r+1] {
if amem[s] != q[r+1] {
continue nextn; continue nextn;
} }
} }
// check the position equals another only if the states are identical // check the position equals another only if the states are identical
for j:=0; j<nstate; j++ { for j := 0; j < nstate; j++ {
if indgo[j] == n { if indgo[j] == n {
// we have some disagreement // we have some disagreement
...@@ -2862,7 +2771,8 @@ stin(i int) ...@@ -2862,7 +2771,8 @@ stin(i int)
indgo[i] = n; indgo[i] = n;
if adb > 1 { if adb > 1 {
fmt.Fprintf(ftable, "State %v: entry at" fmt.Fprintf(ftable, "State %v: entry at"
"%v equals state %v\n", i, n, j); "%v equals state %v\n",
i, n, j);
} }
return; return;
} }
...@@ -2873,7 +2783,7 @@ stin(i int) ...@@ -2873,7 +2783,7 @@ stin(i int)
} }
for r := 0; r < nq; r += 2 { for r := 0; r < nq; r += 2 {
s = q[r] + n; s = q[r]+n;
if s > maxa { if s > maxa {
maxa = s; maxa = s;
} }
...@@ -2895,10 +2805,8 @@ stin(i int) ...@@ -2895,10 +2805,8 @@ stin(i int)
// this version is for limbo // this version is for limbo
// write out the optimized parser // write out the optimized parser
// //
func func aoutput() {
aoutput() fmt.Fprintf(ftable, "const\tYYLAST\t= %v\n", maxa+1);
{
fmt.Fprintf(ftable, "const\tYYLAST\t= %v\n",maxa+1);
arout("YYACT", amem, maxa+1); arout("YYACT", amem, maxa+1);
arout("YYPACT", indgo, nstate); arout("YYPACT", indgo, nstate);
arout("YYPGO", pgo, nnonter+1); arout("YYPGO", pgo, nnonter+1);
...@@ -2907,9 +2815,7 @@ aoutput() ...@@ -2907,9 +2815,7 @@ aoutput()
// //
// put out other arrays, copy the parsers // put out other arrays, copy the parsers
// //
func func others() {
others()
{
var i, j int; var i, j int;
arout("YYR1", levprd, nprod); arout("YYR1", levprd, nprod);
...@@ -2918,19 +2824,19 @@ others() ...@@ -2918,19 +2824,19 @@ others()
// //
//yyr2 is the number of rules for each production //yyr2 is the number of rules for each production
// //
for i=1; i<nprod; i++ { for i = 1; i < nprod; i++ {
temp1[i] = len(prdptr[i]) - 2; temp1[i] = len(prdptr[i])-2;
} }
arout("YYR2", temp1, nprod); arout("YYR2", temp1, nprod);
aryfil(temp1, nstate, -1000); aryfil(temp1, nstate, -1000);
for i=0; i<=ntokens; i++ { for i = 0; i <= ntokens; i++ {
for j:=tstates[i]; j!=0; j=mstates[j] { for j := tstates[i]; j != 0; j = mstates[j] {
temp1[j] = i; temp1[j] = i;
} }
} }
for i=0; i<=nnonter; i++ { for i = 0; i <= nnonter; i++ {
for j=ntstates[i]; j!=0; j=mstates[j] { for j = ntstates[i]; j != 0; j = mstates[j] {
temp1[j] = -i; temp1[j] = -i;
} }
} }
...@@ -2941,7 +2847,7 @@ others() ...@@ -2941,7 +2847,7 @@ others()
// table 1 has 0-256 // table 1 has 0-256
aryfil(temp1, 256, 0); aryfil(temp1, 256, 0);
c := 0; c := 0;
for i=1; i<=ntokens; i++ { for i = 1; i <= ntokens; i++ {
j = tokset[i].value; j = tokset[i].value;
if j >= 0 && j < 256 { if j >= 0 && j < 256 {
if temp1[j] != 0 { if temp1[j] != 0 {
...@@ -2965,7 +2871,7 @@ others() ...@@ -2965,7 +2871,7 @@ others()
// table 2 has PRIVATE-PRIVATE+256 // table 2 has PRIVATE-PRIVATE+256
aryfil(temp1, 256, 0); aryfil(temp1, 256, 0);
c = 0; c = 0;
for i=1; i<=ntokens; i++ { for i = 1; i <= ntokens; i++ {
j = tokset[i].value - PRIVATE; j = tokset[i].value - PRIVATE;
if j >= 0 && j < 256 { if j >= 0 && j < 256 {
if temp1[j] != 0 { if temp1[j] != 0 {
...@@ -2984,7 +2890,7 @@ others() ...@@ -2984,7 +2890,7 @@ others()
// table 3 has everything else // table 3 has everything else
fmt.Fprintf(ftable, "var\tYYTOK3\t= []int {\n"); fmt.Fprintf(ftable, "var\tYYTOK3\t= []int {\n");
c = 0; c = 0;
for i=1; i<=ntokens; i++ { for i = 1; i <= ntokens; i++ {
j = tokset[i].value; j = tokset[i].value;
if j >= 0 && j < 256 { if j >= 0 && j < 256 {
continue; continue;
...@@ -3012,9 +2918,7 @@ others() ...@@ -3012,9 +2918,7 @@ others()
fmt.Fprintf(ftable, "%v", yaccpar); fmt.Fprintf(ftable, "%v", yaccpar);
} }
func func arout(s string, v []int, n int) {
arout(s string, v []int, n int)
{
fmt.Fprintf(ftable, "var\t%v\t= []int {\n", s); fmt.Fprintf(ftable, "var\t%v\t= []int {\n", s);
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
if i%10 == 0 { if i%10 == 0 {
...@@ -3029,10 +2933,8 @@ arout(s string, v []int, n int) ...@@ -3029,10 +2933,8 @@ arout(s string, v []int, n int)
// //
// output the summary on y.output // output the summary on y.output
// //
func func summary() {
summary() if foutput != nil {
{
if(foutput != nil) {
fmt.Fprintf(foutput, "\n%v terminals, %v nonterminals\n", ntokens, nnonter+1); fmt.Fprintf(foutput, "\n%v terminals, %v nonterminals\n", ntokens, nnonter+1);
fmt.Fprintf(foutput, "%v grammar rules, %v/%v states\n", nprod, nstate, NSTATES); fmt.Fprintf(foutput, "%v grammar rules, %v/%v states\n", nprod, nstate, NSTATES);
fmt.Fprintf(foutput, "%v shift/reduce, %v reduce/reduce conflicts reported\n", zzsrconf, zzrrconf); fmt.Fprintf(foutput, "%v shift/reduce, %v reduce/reduce conflicts reported\n", zzsrconf, zzrrconf);
...@@ -3061,9 +2963,7 @@ summary() ...@@ -3061,9 +2963,7 @@ summary()
// //
// write optimizer summary // write optimizer summary
// //
func func osummary() {
osummary()
{
if foutput == nil { if foutput == nil {
return; return;
} }
...@@ -3082,43 +2982,33 @@ osummary() ...@@ -3082,43 +2982,33 @@ osummary()
// //
// copies and protects "'s in q // copies and protects "'s in q
// //
func func chcopy(q string) string {
chcopy(q string) string
{
s := ""; s := "";
i := 0; i := 0;
j := 0; j := 0;
for i = 0; i < len(q); i++ { for i = 0; i < len(q); i++ {
if q[i] == '"' { if q[i] == '"' {
s += q[j:i] + "\\"; s += q[j:i]+"\\";
j = i; j = i;
} }
} }
return s + q[j:i]; return s+q[j:i];
} }
func func usage() {
usage()
{
fmt.Fprintf(stderr, "usage: gacc [-o output] [-v parsetable] input\n"); fmt.Fprintf(stderr, "usage: gacc [-o output] [-v parsetable] input\n");
exit(1); exit(1);
} }
func func bitset(set Lkset, bit int) int {
bitset(set Lkset, bit int) int return set[bit>>5]&(1<<uint(bit&31));
{
return set[bit>>5] & (1<<uint(bit&31));
} }
func func setbit(set Lkset, bit int) {
setbit(set Lkset, bit int)
{
set[bit>>5] |= (1<<uint(bit&31)); set[bit>>5] |= (1<<uint(bit&31));
} }
func func mkset() Lkset {
mkset() Lkset
{
return make([]int, tbitset); return make([]int, tbitset);
} }
...@@ -3126,13 +3016,11 @@ mkset() Lkset ...@@ -3126,13 +3016,11 @@ mkset() Lkset
// set a to the union of a and b // set a to the union of a and b
// return 1 if b is not a subset of a, 0 otherwise // return 1 if b is not a subset of a, 0 otherwise
// //
func func setunion(a, b []int) int {
setunion(a, b []int) int
{
sub := 0; sub := 0;
for i:=0; i<tbitset; i++ { for i := 0; i < tbitset; i++ {
x := a[i]; x := a[i];
y := x | b[i]; y := x|b[i];
a[i] = y; a[i] = y;
if y != x { if y != x {
sub = 1; sub = 1;
...@@ -3141,15 +3029,13 @@ setunion(a, b []int) int ...@@ -3141,15 +3029,13 @@ setunion(a, b []int) int
return sub; return sub;
} }
func func prlook(p Lkset) {
prlook(p Lkset)
{
if p == nil { if p == nil {
fmt.Fprintf(foutput, "\tNULL"); fmt.Fprintf(foutput, "\tNULL");
return; return;
} }
fmt.Fprintf(foutput, " { "); fmt.Fprintf(foutput, " { ");
for j:=0; j<=ntokens; j++ { for j := 0; j <= ntokens; j++ {
if bitset(p, j) != 0 { if bitset(p, j) != 0 {
fmt.Fprintf(foutput, "%v ", symnam(j)); fmt.Fprintf(foutput, "%v ", symnam(j));
} }
...@@ -3160,23 +3046,17 @@ prlook(p Lkset) ...@@ -3160,23 +3046,17 @@ prlook(p Lkset)
// //
// utility routines // utility routines
// //
var peekrune int; var peekrune int
func func isdigit(c int) bool {
isdigit(c int) bool
{
return c >= '0' && c <= '9'; return c >= '0' && c <= '9';
} }
func func isword(c int) bool {
isword(c int) bool
{
return c >= 0xa0 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); return c >= 0xa0 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
} }
func func mktemp(t string) string {
mktemp(t string) string
{
return t; return t;
} }
...@@ -3184,14 +3064,12 @@ mktemp(t string) string ...@@ -3184,14 +3064,12 @@ mktemp(t string) string
// return 1 if 2 arrays are equal // return 1 if 2 arrays are equal
// return 0 if not equal // return 0 if not equal
// //
func func aryeq(a []int, b []int) int {
aryeq(a []int, b[]int) int
{
n := len(a); n := len(a);
if len(b) != n { if len(b) != n {
return 0; return 0;
} }
for ll:=0; ll<n; ll++ { for ll := 0; ll < n; ll++ {
if a[ll] != b[ll] { if a[ll] != b[ll] {
return 0; return 0;
} }
...@@ -3199,18 +3077,14 @@ aryeq(a []int, b[]int) int ...@@ -3199,18 +3077,14 @@ aryeq(a []int, b[]int) int
return 1; return 1;
} }
func func putrune(f *bufio.Writer, c int) {
putrune(f *bufio.Writer, c int)
{
s := string(c); s := string(c);
for i:=0; i<len(s); i++ { for i := 0; i < len(s); i++ {
f.WriteByte(s[i]); f.WriteByte(s[i]);
} }
} }
func func getrune(f *bufio.Reader) int {
getrune(f *bufio.Reader) int
{
var r int; var r int;
if peekrune != 0 { if peekrune != 0 {
...@@ -3222,20 +3096,18 @@ getrune(f *bufio.Reader) int ...@@ -3222,20 +3096,18 @@ getrune(f *bufio.Reader) int
return r; return r;
} }
c,n,err := f.ReadRune(); c, n, err := f.ReadRune();
if n == 0 { if n == 0 {
return EOF; return EOF;
} }
if err != nil { if err != nil {
error("read error: %v", err); error("read error: %v", err);
} }
//fmt.Printf("rune = %v n=%v\n", string(c), n); //fmt.Printf("rune = %v n=%v\n", string(c), n);
return c; return c;
} }
func func ungetrune(f *bufio.Reader, c int) {
ungetrune(f *bufio.Reader, c int)
{
if f != finput { if f != finput {
panic("ungetc - not finput"); panic("ungetc - not finput");
} }
...@@ -3245,41 +3117,33 @@ ungetrune(f *bufio.Reader, c int) ...@@ -3245,41 +3117,33 @@ ungetrune(f *bufio.Reader, c int)
peekrune = c; peekrune = c;
} }
func func write(f *bufio.Writer, b []byte, n int) int {
write(f *bufio.Writer, b []byte, n int) int
{
println("write"); println("write");
return 0; return 0;
} }
func func open(s string) *bufio.Reader {
open(s string) *bufio.Reader fi, err := os.Open(s, os.O_RDONLY, 0);
{
fi,err := os.Open(s, os.O_RDONLY, 0);
if err != nil { if err != nil {
error("error opening %v: %v", s, err); error("error opening %v: %v", s, err);
} }
//fmt.Printf("open %v\n", s); //fmt.Printf("open %v\n", s);
return bufio.NewReader(fi); return bufio.NewReader(fi);
} }
func func create(s string, m int) *bufio.Writer {
create(s string, m int) *bufio.Writer fo, err := os.Open(s, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, m);
{
fo,err := os.Open(s, os.O_WRONLY|os.O_CREAT|os.O_TRUNC, m);
if err != nil { if err != nil {
error("error opening %v: %v", s, err); error("error opening %v: %v", s, err);
} }
//fmt.Printf("create %v mode %v\n", s, m); //fmt.Printf("create %v mode %v\n", s, m);
return bufio.NewWriter(fo); return bufio.NewWriter(fo);
} }
// //
// write out error comment // write out error comment
// //
func func error(s string, v ...) {
error(s string, v ...)
{
nerrors++; nerrors++;
fmt.Fprintf(stderr, s, v); fmt.Fprintf(stderr, s, v);
fmt.Fprintf(stderr, ": %v:%v\n", infile, lineno); fmt.Fprintf(stderr, ": %v:%v\n", infile, lineno);
...@@ -3289,9 +3153,7 @@ error(s string, v ...) ...@@ -3289,9 +3153,7 @@ error(s string, v ...)
} }
} }
func func exit(status int) {
exit(status int)
{
if ftable != nil { if ftable != nil {
ftable.Flush(); ftable.Flush();
ftable = nil; ftable = nil;
...@@ -3307,7 +3169,7 @@ exit(status int) ...@@ -3307,7 +3169,7 @@ exit(status int)
os.Exit(status); os.Exit(status);
} }
var yaccpar = var yaccpar =
// from here to the end of the file is // from here to the end of the file is
// a single string containing the old yaccpar file // a single string containing the old yaccpar file
` `
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment