From 054354ebd410d14c023cc21d011d1bd6632f91f3 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Thu, 5 Nov 2009 22:29:29 -0800 Subject: [PATCH] gofmt src/cmd/goyacc (with this change: gofmt -l src/cmd/ | wc is 0 0 0) R=ken, rsc http://go/go-review/1024005 --- src/cmd/goyacc/goyacc.go | 1028 +++++++++++++++++--------------------- 1 file changed, 445 insertions(+), 583 deletions(-) diff --git a/src/cmd/goyacc/goyacc.go b/src/cmd/goyacc/goyacc.go index 763e61cb03..7717cc8395 100644 --- a/src/cmd/goyacc/goyacc.go +++ b/src/cmd/goyacc/goyacc.go @@ -44,8 +44,7 @@ package main // major difference is lack of stem ("y" variable) // -import -( +import ( "flag"; "fmt"; "bufio"; @@ -54,66 +53,61 @@ import // the following are adjustable // according to memory size -const -( +const ( ACTSIZE = 30000; NSTATES = 2000; TEMPSIZE = 2000; - SYMINC = 50; // increase for non-term or term - RULEINC = 50; // increase for max rule length prodptr[i] - PRODINC = 100; // increase for productions prodptr - WSETINC = 50; // increase for working sets wsets - STATEINC = 200; // increase for states statemem + SYMINC = 50; // increase for non-term or term + RULEINC = 50; // increase for max rule length prodptr[i] + PRODINC = 100; // increase for productions prodptr + WSETINC = 50; // increase for working sets wsets + STATEINC = 200; // increase for states statemem NAMESIZE = 50; NTYPES = 63; ISIZE = 400; - PRIVATE = 0xE000; // unicode private use + PRIVATE = 0xE000; // unicode private use -// relationships which must hold: -// TEMPSIZE >= NTERMS + NNONTERM + 1; -// TEMPSIZE >= NSTATES; -// + // relationships which must hold: + // TEMPSIZE >= NTERMS + NNONTERM + 1; + // TEMPSIZE >= NSTATES; + // NTBASE = 010000; ERRCODE = 8190; ACCEPTCODE = 8191; YYLEXUNK = 3; - TOKSTART = 4; //index of first defined token + TOKSTART = 4; //index of first defined token ) // no, left, right, binary assoc. -const -( - NOASC = iota; +const ( + NOASC = iota; LASC; RASC; BASC; ) // flags for state generation -const -( - DONE = iota; +const ( + DONE = iota; MUSTDO; MUSTLOOKAHEAD; ) // flags for a rule having an action, and being reduced -const -( - ACTFLAG = 1<<(iota+2); +const ( + ACTFLAG = 1<<(iota+2); REDFLAG; ) // output parser flags -const YYFLAG = -1000 +const YYFLAG = -1000 // parse tokens -const -( +const ( IDENTIFIER = PRIVATE+iota; MARK; TERM; @@ -130,246 +124,222 @@ const UNION; ) -const ENDFILE = 0 -const EMPTY = 1 -const WHOKNOWS = 0 -const OK = 1 -const NOMORE = -1000 +const ENDFILE = 0 +const EMPTY = 1 +const WHOKNOWS = 0 +const OK = 1 +const NOMORE = -1000 // macros for getting associativity and precedence levels -func -ASSOC(i int) int -{ - return i & 3; +func ASSOC(i int) int { + return i&3; } -func -PLEVEL(i int) int -{ - return (i >> 4) & 077; +func PLEVEL(i int) int { + return (i>>4)&077; } -func -TYPE(i int) int -{ - return (i >> 10) & 077; +func TYPE(i int) int { + return (i>>10)&077; } // macros for setting associativity and precedence levels -func -SETASC(i, j int) int -{ - return i | j; +func SETASC(i, j int) int { + return i|j; } -func -SETPLEV(i, j int) int -{ - return i | (j << 4); +func SETPLEV(i, j int) int { + return i|(j<<4); } -func -SETTYPE(i, j int) int -{ - return i | (j << 10); +func SETTYPE(i, j int) int { + return i|(j<<10); } // I/O descriptors -var finput *bufio.Reader // input file -var stderr *bufio.Writer -var ftable *bufio.Writer // y.go file -var foutput *bufio.Writer // y.output file +var finput *bufio.Reader // input file +var stderr *bufio.Writer +var ftable *bufio.Writer // y.go file +var foutput *bufio.Writer // y.output file -var oflag string // -o [y.go] - y.go file -var vflag string // -v [y.output] - y.output file -var lflag bool // -l - disable line directives +var oflag string // -o [y.go] - y.go file +var vflag string // -v [y.output] - y.output file +var lflag bool // -l - disable line directives -var stacksize = 200 +var stacksize = 200 // communication variables between various I/O routines -var infile string // input file name -var numbval int // value of an input number -var tokname string // input token name, slop for runes and 0 -var tokflag = false; +var infile string // input file name +var numbval int // value of an input number +var tokname string // input token name, slop for runes and 0 +var tokflag = false // structure declarations -type Lkset []int +type Lkset []int -type Pitem -struct -{ - prod []int; - off int; // offset within the production - first int; // first term or non-term in item - prodno int; // production number for sorting +type Pitem struct { + prod []int; + off int; // offset within the production + first int; // first term or non-term in item + prodno int; // production number for sorting } -type Item -struct -{ - pitem Pitem; - look Lkset; +type Item struct { + pitem Pitem; + look Lkset; } -type Symb -struct -{ - name string; - value int; +type Symb struct { + name string; + value int; } -type Wset -struct -{ - pitem Pitem; - flag int; - ws Lkset; +type Wset struct { + pitem Pitem; + flag int; + ws Lkset; } // storage of types -var ntypes int // number of types defined -var typeset [NTYPES]string // pointers to type tags +var ntypes int // number of types defined +var typeset [NTYPES]string // pointers to type tags // token information -var ntokens = 0 // number of tokens -var tokset []Symb -var toklev []int // vector with the precedence of the terminals +var ntokens = 0 // number of tokens +var tokset []Symb +var toklev []int // vector with the precedence of the terminals // nonterminal information -var nnonter = -1 // the number of nonterminals -var nontrst []Symb -var start int // start symbol +var nnonter = -1 // the number of nonterminals +var nontrst []Symb +var start int // start symbol // state information -var nstate = 0 // number of states -var pstate = make([]int, NSTATES+2) // index into statemem to the descriptions of the states -var statemem []Item -var tystate = make([]int, NSTATES) // contains type information about the states -var tstates []int // states generated by terminal gotos -var ntstates []int // states generated by nonterminal gotos -var mstates = make([]int, NSTATES) // chain of overflows of term/nonterm generation lists -var lastred int // number of last reduction of a state -var defact = make([]int, NSTATES) // default actions of states +var nstate = 0 // number of states +var pstate = make([]int, NSTATES+2) // index into statemem to the descriptions of the states +var statemem []Item +var tystate = make([]int, NSTATES) // contains type information about the states +var tstates []int // states generated by terminal gotos +var ntstates []int // states generated by nonterminal gotos +var mstates = make([]int, NSTATES) // chain of overflows of term/nonterm generation lists +var lastred int // number of last reduction of a state +var defact = make([]int, NSTATES) // default actions of states // lookahead set information -var lkst []Lkset -var nolook = 0 // flag to turn off lookahead computations -var tbitset = 0 // size of lookahead sets -var clset Lkset // temporary storage for lookahead computations +var lkst []Lkset +var nolook = 0 // flag to turn off lookahead computations +var tbitset = 0 // size of lookahead sets +var clset Lkset // temporary storage for lookahead computations // working set information -var wsets []Wset -var cwp int +var wsets []Wset +var cwp int // storage for action table -var amem []int // action table storage -var memp int // next free action table position -var indgo = make([]int, NSTATES) // index to the stored goto table +var amem []int // action table storage +var memp int // next free action table position +var indgo = make([]int, NSTATES) // index to the stored goto table // temporary vector, indexable by states, terms, or ntokens -var temp1 = make([]int, TEMPSIZE) // temporary storage, indexed by terms + ntokens or states -var lineno = 1 // current input line number -var fatfl = 1 // if on, error is fatal -var nerrors = 0 // number of errors +var temp1 = make([]int, TEMPSIZE) // temporary storage, indexed by terms + ntokens or states +var lineno = 1 // current input line number +var fatfl = 1 // if on, error is fatal +var nerrors = 0 // number of errors // assigned token type values -var extval = 0 +var extval = 0 // grammar rule information -var nprod = 1 // number of productions -var prdptr [][]int // pointers to descriptions of productions -var levprd []int // precedence levels for the productions -var rlines []int // line number for this rule +var nprod = 1 // number of productions +var prdptr [][]int // pointers to descriptions of productions +var levprd []int // precedence levels for the productions +var rlines []int // line number for this rule // statistics collection variables -var zzgoent = 0 -var zzgobest = 0 -var zzacent = 0 -var zzexcp = 0 -var zzclose = 0 -var zzrrconf = 0 -var zzsrconf = 0 -var zzstate = 0 +var zzgoent = 0 +var zzgobest = 0 +var zzacent = 0 +var zzexcp = 0 +var zzclose = 0 +var zzrrconf = 0 +var zzsrconf = 0 +var zzstate = 0 // optimizer arrays -var yypgo [][]int -var optst [][]int -var ggreed []int -var pgo []int +var yypgo [][]int +var optst [][]int +var ggreed []int +var pgo []int -var maxspr int // maximum spread of any entry -var maxoff int // maximum offset into a array -var maxa int +var maxspr int // maximum spread of any entry +var maxoff int // maximum offset into a array +var maxa int // storage for information about the nonterminals -var pres [][][]int // vector of pointers to productions yielding each nonterminal -var pfirst []Lkset -var pempty []int // vector of nonterminals nontrivially deriving e +var pres [][][]int // vector of pointers to productions yielding each nonterminal +var pfirst []Lkset +var pempty []int // vector of nonterminals nontrivially deriving e // random stuff picked out from between functions -var indebug = 0 // debugging flag for cpfir -var pidebug = 0 // debugging flag for putitem -var gsdebug = 0 // debugging flag for stagen -var cldebug = 0 // debugging flag for closure -var pkdebug = 0 // debugging flag for apack -var g2debug = 0 // debugging for go2gen -var adb = 0 // debugging for callopt +var indebug = 0 // debugging flag for cpfir +var pidebug = 0 // debugging flag for putitem +var gsdebug = 0 // debugging flag for stagen +var cldebug = 0 // debugging flag for closure +var pkdebug = 0 // debugging flag for apack +var g2debug = 0 // debugging for go2gen +var adb = 0 // debugging for callopt -type Resrv -struct -{ +type Resrv struct { name string; value int; } -var resrv = -[]Resrv { - Resrv{"binary", BINARY}, - Resrv{"left", LEFT}, - Resrv{"nonassoc", BINARY}, - Resrv{"prec", PREC}, - Resrv{"right", RIGHT}, - Resrv{"start", START}, - Resrv{"term", TERM}, - Resrv{"token", TERM}, - Resrv{"type", TYPEDEF}, - Resrv{"union", UNION}, - Resrv{"struct", UNION} +var resrv = []Resrv{ + Resrv{"binary", BINARY}, + Resrv{"left", LEFT}, + Resrv{"nonassoc", BINARY}, + Resrv{"prec", PREC}, + Resrv{"right", RIGHT}, + Resrv{"start", START}, + Resrv{"term", TERM}, + Resrv{"token", TERM}, + Resrv{"type", TYPEDEF}, + Resrv{"union", UNION}, + Resrv{"struct", UNION}, } -var zznewstate = 0 -const EOF = -1 -const UTFmax = 0x3f +var zznewstate = 0 -func -main() -{ +const EOF = -1 +const UTFmax = 0x3f - setup(); // initialize and read productions +func main() { + + setup(); // initialize and read productions tbitset = (ntokens+32)/32; - cpres(); // make table of which productions yield a given nonterminal - cempty(); // make a table of which nonterminals can match the empty string - cpfir(); // make a table of firsts of nonterminals + cpres(); // make table of which productions yield a given nonterminal + cempty(); // make a table of which nonterminals can match the empty string + cpfir(); // make a table of firsts of nonterminals - stagen(); // generate the states + stagen(); // generate the states yypgo = make([][]int, nnonter+1); optst = make([][]int, nstate); - output(); // write the states and the tables + output(); // write the states and the tables go2out(); hideprod(); @@ -382,9 +352,7 @@ main() exit(0); } -func -setup() -{ +func setup() { var j, ty int; stderr = bufio.NewWriter(os.NewFile(2, "stderr")); @@ -414,13 +382,13 @@ setup() t := gettok(); - outer: +outer: for { switch t { default: error("syntax error tok=%v", t-PRIVATE); - case MARK,ENDFILE: + case MARK, ENDFILE: break outer; case ';': @@ -445,18 +413,20 @@ setup() t = chfind(1, tokname); if t < NTBASE { j = TYPE(toklev[t]); - if(j != 0 && j != ty) { + if j != 0 && j != ty { error("type redeclaration of token ", tokset[t].name); - } else + } else { toklev[t] = SETTYPE(toklev[t], ty); + } } else { j = nontrst[t-NTBASE].value; - if(j != 0 && j != ty) { + if j != 0 && j != ty { error("type redeclaration of nonterminal %v", nontrst[t-NTBASE].name); - } else + } else { nontrst[t-NTBASE].value = ty; + } } continue; @@ -470,7 +440,7 @@ setup() case UNION: cpyunion(); - case LEFT,BINARY,RIGHT,TERM: + case LEFT, BINARY, RIGHT, TERM: // nonzero means new prec. and assoc. lev := t-TERM; if lev != 0 { @@ -492,7 +462,7 @@ setup() t = gettok(); continue; - case';': + case ';': break; case IDENTIFIER: @@ -536,7 +506,7 @@ setup() } // put out non-literal terminals - for i:=TOKSTART; i<=ntokens; i++ { + for i := TOKSTART; i <= ntokens; i++ { // non-literals c := tokset[i].name[0]; if c != ' ' && c != '$' { @@ -546,16 +516,16 @@ setup() // put out names of token names 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, "}\n"); // put out names of state names fmt.Fprintf(ftable, "var\tStatenames\t =[]string {\n"); -// for i:=TOKSTART; i<=ntokens; i++ { -// fmt.Fprintf(ftable, "\t\"%v\",\n", tokset[i].name); -// } + // for i:=TOKSTART; i<=ntokens; i++ { + // fmt.Fprintf(ftable, "\t\"%v\",\n", tokset[i].name); + // } fmt.Fprintf(ftable, "}\n"); fmt.Fprintf(ftable, "\nfunc\n"); @@ -563,7 +533,7 @@ setup() fmt.Fprintf(ftable, "switch p {\n"); moreprod(); - prdptr[0] = []int{NTBASE,start,1,0}; + prdptr[0] = []int{NTBASE, start, 1, 0}; nprod = 1; curprod := make([]int, RULEINC); @@ -590,15 +560,15 @@ setup() if t == '|' { curprod[mem] = prdptr[nprod-1][0]; mem++; - } else - if t == IDENTCOLON { + } else if t == IDENTCOLON { curprod[mem] = chfind(1, tokname); if curprod[mem] < NTBASE { error("token illegal on LHS of grammar rule"); } mem++; - } else + } else { error("illegal rule: missing semicolon or | ?"); + } // read rule body t = gettok(); @@ -611,7 +581,7 @@ setup() mem++; if mem >= len(curprod) { ncurprod := make([]int, mem+RULEINC); - for ll:=0; ll= NTBASE { - error("nonterminal "+nontrst[j-NTBASE].name+" illegal after %%prec"); + error("nonterminal " + nontrst[j-NTBASE].name + " illegal after %%prec"); } levprd[nprod] = toklev[j]; t = gettok(); @@ -640,7 +610,7 @@ setup() t = gettok(); if t == IDENTIFIER { // 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 @@ -662,7 +632,7 @@ setup() mem++; if mem >= len(curprod) { ncurprod := make([]int, mem+RULEINC); - for ll:=0; ll= NTBASE { tempty = nontrst[tempty-NTBASE].value; - } else + } else { tempty = TYPE(toklev[tempty]); + } if tempty != nontrst[curprod[0]-NTBASE].value { error("default action causes potential type clash"); } @@ -697,7 +668,7 @@ setup() } moreprod(); prdptr[nprod] = make([]int, mem); - for ll:=0; ll= n { nn := n+PRODINC; @@ -747,7 +716,7 @@ moreprod() alevprd := make([]int, nn); arlines := make([]int, nn); - for ll:=0; ll= len(nontrst) { anontrst := make([]Symb, nnonter+SYMINC); - for ll:=0; ll= '0' && c <= '9': c -= '0'; case c >= 'a' && c <= 'f': - c -= 'a' - 10; + c -= 'a'-10; case c >= 'A' && c <= 'F': - c -= 'A' - 10; + c -= 'A'-10; default: error("illegal \\unnnn construction"); } - val = val * 16 + c; + val = val*16 + c; s = s[1:len(s)]; } if val == 0 { error("'\\u0000' is illegal"); } - } else + } else { error("unknown escape"); + } } else { val = extval; extval++; @@ -852,10 +827,9 @@ defin(nt int, s string) int return ntokens; } -var peekline = 0; -func -gettok() int -{ +var peekline = 0 + +func gettok() int { var i, match, c int; tokname = ""; @@ -903,7 +877,7 @@ gettok() int error("unterminated < ... > clause"); } - for i=1; i<=ntypes; i++ { + for i = 1; i <= ntypes; i++ { if typeset[i] == tokname { numbval = i; if tokflag { @@ -926,13 +900,12 @@ gettok() int for { c = getrune(finput); if c == '\n' || c == EOF { - error("illegal or missing ' or \"" ); + error("illegal or missing ' or \""); } if c == '\\' { tokname += string('\\'); c = getrune(finput); - } else - if c == match { + } else if c == match { if tokflag { fmt.Printf(">>> IDENTIFIER \"%v\" %v\n", tokname, lineno); } @@ -963,11 +936,11 @@ gettok() int getword(c); // find a reserved word - for c=0; c < len(resrv); c++ { + for c = 0; c < len(resrv); c++ { if tokname == resrv[c].name { if tokflag { fmt.Printf(">>> %%%v %v %v\n", tokname, - resrv[c].value-PRIVATE, lineno); + resrv[c].value - PRIVATE, lineno); } return resrv[c].value; } @@ -975,13 +948,13 @@ gettok() int error("invalid escape, or illegal reserved word: %v", tokname); case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': - numbval = c - '0'; + numbval = c-'0'; for { c = getrune(finput); if !isdigit(c) { break; } - numbval = numbval*10 + c-'0'; + numbval = numbval*10 + c - '0'; } ungetrune(finput, c); if tokflag { @@ -1026,9 +999,7 @@ gettok() int return IDENTIFIER; } -func -getword(c int) -{ +func getword(c int) { tokname = ""; for isword(c) || isdigit(c) || c == '_' || c == '.' || c == '$' { tokname += string(c); @@ -1040,9 +1011,7 @@ getword(c int) // // determine the type of a symbol // -func -fdtype(t int) int -{ +func fdtype(t int) int { var v int; var s string; @@ -1059,18 +1028,16 @@ fdtype(t int) int return v; } -func -chfind(t int, s string) int -{ +func chfind(t int, s string) int { if s[0] == ' ' { t = 0; } - for i:=0; i<=ntokens; i++ { + for i := 0; i <= ntokens; i++ { if s == tokset[i].name { return i; } } - for i:=0; i<=nnonter; i++ { + for i := 0; i <= nnonter; i++ { if s == nontrst[i].name { return NTBASE+i; } @@ -1086,9 +1053,7 @@ chfind(t int, s string) int // // copy the union declaration to the output, and the define file if present // -func -cpyunion() -{ +func cpyunion() { if !lflag { fmt.Fprintf(ftable, "\n//line %v %v\n", lineno, infile); @@ -1097,14 +1062,14 @@ cpyunion() level := 0; - out: +out: for { c := getrune(finput); if c == EOF { error("EOF encountered while processing %%union"); } putrune(ftable, c); - switch(c) { + switch c { case '\n': lineno++; case '{': @@ -1128,9 +1093,7 @@ cpyunion() // // saves code between %{ and %} // -func -cpycode() -{ +func cpycode() { lno := lineno; c := getrune(finput); @@ -1231,9 +1194,7 @@ cpycode() // skip over comments // skipcom is called after reading a '/' // -func -skipcom() int -{ +func skipcom() int { var c int; c = getrune(finput); @@ -1254,7 +1215,7 @@ skipcom() int nl := 0; // lines skipped c = getrune(finput); - l1: +l1: switch c { case '*': c = getrune(finput); @@ -1274,25 +1235,22 @@ skipcom() int return nl; } -func -dumpprod(curprod []int, max int) -{ +func dumpprod(curprod []int, max int) { fmt.Printf("\n"); - for i:=0; i= max { error("Illegal use of $%v", j); } - } else - if isword(c) || c == '_' || c == '.' { + } else if isword(c) || c == '_' || c == '.' { // look for $name ungetrune(finput, c); if gettok() != IDENTIFIER { @@ -1372,12 +1329,12 @@ cpyact(curprod []int, max int) c = getrune(finput); if c != '@' { ungetrune(finput, c); - } else - if gettok() != NUMBER { + } else if gettok() != NUMBER { error("@ must be followed by number"); - } else + } else { fnd = numbval; - for j=1; j= NTBASE { s = nontrst[i-NTBASE].name; - } else + } else { s = tokset[i].name; + } if s[0] == ' ' { s = s[1:len(s)]; } @@ -1517,10 +1470,8 @@ symnam(i int) string // // set elements 0 through n-1 to c // -func -aryfil(v []int, n, c int) -{ - for i:=0; i= NTBASE && pempty[prd[p]-NTBASE] == WHOKNOWS { break; @@ -1628,7 +1573,7 @@ cempty() } // 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 ... if i == 0 { continue; @@ -1650,16 +1595,16 @@ cempty() // loop as long as we keep finding empty nonterminals - again: +again: for { - next: - for i=1; i= len(wsets) { awsets := make([]Wset, cwp+WSETINC); - for ll:=0; ll p1; l-- { - if(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) { + if 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 { s := statemem[l]; statemem[l] = statemem[l-1]; statemem[l-1] = s; - } else + } else { break; + } } } - size1 := p2 - p1; // size of state + size1 := p2-p1; // size of state var i int; if c >= NTBASE { i = ntstates[c-NTBASE]; - } else + } else { i = tstates[c]; + } - look: +look: for ; i != 0; i = mstates[i] { // get ith state q1 := pstate[i]; q2 := pstate[i+1]; - size2 := q2 - q1; + size2 := q2-q1; if size1 != size2 { continue; } k = p1; for l = q1; l < q2; l++ { 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; } k++; @@ -2081,9 +2019,7 @@ state(c int) int return nstate-1; } -func -putitem(p Pitem, set Lkset) -{ +func putitem(p Pitem, set Lkset) { p.off++; p.first = p.prod[p.off]; @@ -2093,7 +2029,7 @@ putitem(p Pitem, set Lkset) j := pstate[nstate+1]; if j >= len(statemem) { asm := make([]Item, j+STATEINC); - for ll:=0; ll n { return 0; } 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 - r := len(amem) - len(p); + r := len(amem)-len(p); - nextk: +nextk: for rr := 0; rr <= r; rr++ { qq := rr; for pp = 0; pp < len(p); pp++ { @@ -2211,7 +2143,7 @@ apack(p []int, n int) int fmt.Fprintf(foutput, "\n"); } } - return off + rr; + return off+rr; } error("no space in action table"); return 0; @@ -2220,9 +2152,7 @@ apack(p []int, n int) int // // print the output for the states // -func -output() -{ +func output() { var c, u, v int; fmt.Fprintf(ftable, "var\tYYEXCA = []int {\n"); @@ -2230,7 +2160,7 @@ output() noset := mkset(); // output the stuff for state i - for i:=0; i 1 && c < NTBASE && temp1[c] == 0 { - for v=u; v NTBASE { + } else if c > NTBASE { c -= NTBASE; - if temp1[c + ntokens] == 0 { + if temp1[c+ntokens] == 0 { temp1[c+ntokens] = amem[indgo[i]+c]; } } @@ -2263,7 +2192,7 @@ output() // now, we have the shifts; look at the reductions lastred = 0; - for u=0; u lastred { temp1[k] = -lastred; } zzrrconf++; - } else + } else { // potential shift/reduce conflict precftn(lastred, k, i); + } } } wract(i); @@ -2311,9 +2240,7 @@ output() // the conflict is in state s // temp1[t] is changed to reflect the action // -func -precftn(r, t, s int) -{ +func precftn(r, t, s int) { var action int; lp := levprd[r]; @@ -2330,15 +2257,15 @@ precftn(r, t, s int) } if PLEVEL(lt) == PLEVEL(lp) { action = ASSOC(lt); - } else - if PLEVEL(lt) > PLEVEL(lp) { - action = RASC; // shift - } else - action = LASC; // reduce + } else if PLEVEL(lt) > PLEVEL(lp) { + action = RASC; // shift + } else { + action = LASC; + } // reduce switch action { - case BASC: // error action + case BASC: // error action temp1[t] = ERRCODE; - case LASC: // reduce + case LASC: // reduce temp1[t] = -r; } } @@ -2347,15 +2274,13 @@ precftn(r, t, s int) // output state i // temp1 has the actions, lastred the default // -func -wract(i int) -{ +func wract(i int) { var p, p1 int; // find the best choice for lastred lastred = 0; ntimes := 0; - for j:=0; j<=ntokens; j++ { + for j := 0; j <= ntokens; j++ { if temp1[j] >= 0 { continue; } @@ -2366,7 +2291,7 @@ wract(i int) count := 0; tred := -temp1[j]; levprd[tred] |= REDFLAG; - for p=0; p<=ntokens; p++ { + for p = 0; p <= ntokens; p++ { if temp1[p]+tred == 0 { count++; } @@ -2388,7 +2313,7 @@ wract(i int) // clear out entries in temp1 which equal lastred // count entries in optst table n := 0; - for p=0; p<=ntokens; p++ { + for p = 0; p <= ntokens; p++ { p1 = temp1[p]; if p1+lastred == 0 { temp1[p] = 0; @@ -2404,16 +2329,14 @@ wract(i int) flag := 0; os := make([]int, n*2); n = 0; - for p=0; p<=ntokens; p++ { + for p = 0; p <= ntokens; p++ { p1 = temp1[p]; if p1 != 0 { if p1 < 0 { p1 = -p1; - } else - if p1 == ACCEPTCODE { + } else if p1 == ACCEPTCODE { p1 = -1; - } else - if p1 == ERRCODE { + } else if p1 == ERRCODE { p1 = 0; } else { os[n] = p; @@ -2441,9 +2364,7 @@ wract(i int) // // writes state i // -func -wrstate(i int) -{ +func wrstate(i int) { var j0, j1, u int; var pp, qq int; @@ -2452,12 +2373,12 @@ wrstate(i int) } fmt.Fprintf(foutput, "\nstate %v\n", i); qq = pstate[i+1]; - for pp=pstate[i]; pp 0 { if j1 == ACCEPTCODE { fmt.Fprintf(foutput, "accept"); - } else - if j1 == ERRCODE { + } else if j1 == ERRCODE { fmt.Fprintf(foutput, "error"); - } else + } else { fmt.Fprintf(foutput, "shift %v", j1); - } else + } + } else { fmt.Fprintf(foutput, "reduce %v (src line %v)", -j1, rlines[-j1]); + } } } @@ -2488,8 +2410,9 @@ wrstate(i int) if lastred != 0 { fmt.Fprintf(foutput, "\n\t. reduce %v (src line %v)\n\n", lastred, rlines[lastred]); - } else + } else { fmt.Fprintf(foutput, "\n\t. error\n\n"); + } // now, output nonterminal actions j1 = ntokens; @@ -2504,9 +2427,7 @@ wrstate(i int) // // output the gotos for the nontermninals // -func -go2out() -{ +func go2out() { for i := 1; i <= nnonter; i++ { go2gen(i); @@ -2545,7 +2466,7 @@ go2out() n++; } } - goent := make([]int, 2*n+1); + goent := make([]int, 2*n + 1); n = 0; for j := 0; j < nstate; j++ { if tystate[j] != 0 && tystate[j] != best { @@ -2571,9 +2492,7 @@ go2out() // // output the gotos for nonterminal c // -func -go2gen(c int) -{ +func go2gen(c int) { var i, cc, p, q int; // first, find nonterminals with gotos on c @@ -2582,15 +2501,15 @@ go2gen(c int) work := 1; for work != 0 { work = 0; - for i=0; i= 0 && temp1[cc] != 0 { // thus, the left side of production i does too cc = prdptr[i][0]-NTBASE; if temp1[cc] == 0 { - work = 1; - temp1[cc] = 1; + work = 1; + temp1[cc] = 1; } } } @@ -2599,7 +2518,7 @@ go2gen(c int) // now, we have temp1[c] = 1 if a goto on c in closure of cc if g2debug != 0 && foutput != nil { 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 { fmt.Fprintf(foutput, "%v ", nontrst[i].name); } @@ -2609,9 +2528,9 @@ go2gen(c int) // now, go through and put gotos into tystate aryfil(tystate, nstate, 0); - for i=0; i= NTBASE { // goto on c is possible @@ -2630,13 +2549,11 @@ go2gen(c int) // the action array is known, we hide the nonterminals // derived by productions in levprd. // -func -hideprod() -{ +func hideprod() { nred := 0; levprd[0] = 0; - for i:=1; i maxoff { maxoff = k; } @@ -2697,8 +2612,8 @@ callopt() // minimum entry index is always 0 v = yypgo[i]; - q = len(v) - 1; - for p = 0; p < q ; p += 2 { + q = len(v)-1; + for p = 0; p < q; p += 2 { ggreed[i] += 2; if v[p] > j { j = v[p]; @@ -2726,8 +2641,9 @@ callopt() for i != NOMORE { if i >= 0 { stin(i); - } else + } else { gin(-i); + } i = nxti(); } @@ -2749,9 +2665,7 @@ callopt() // // finds the next i // -func -nxti() int -{ +func nxti() int { max := 0; maxi := 0; for i := 1; i <= nnonter; i++ { @@ -2772,25 +2686,23 @@ nxti() int return maxi; } -func -gin(i int) -{ +func gin(i int) { var s int; // enter gotos on nonterminal i into array amem ggreed[i] = 0; q := yypgo[i]; - nq := len(q) - 1; + nq := len(q)-1; // now, find amem place for it - nextgp: +nextgp: for p := 0; p < ACTSIZE; p++ { if amem[p] != 0 { continue; } for r := 0; r < nq; r += 2 { - s = p + q[r] + 1; + s = p+q[r]+1; if s > maxa { maxa = s; if maxa >= ACTSIZE { @@ -2808,7 +2720,7 @@ gin(i int) maxa = p; } for r := 0; r < nq; r += 2 { - s = p + q[r] + 1; + s = p+q[r]+1; amem[s] = q[r+1]; } pgo[i] = p; @@ -2820,9 +2732,7 @@ gin(i int) error("cannot place goto %v\n", i); } -func -stin(i int) -{ +func stin(i int) { var s int; tystate[i] = 0; @@ -2831,25 +2741,24 @@ stin(i int) q := optst[i]; nq := len(q); - nextn: +nextn: // find an acceptable place for n := -maxoff; n < ACTSIZE; n++ { flag := 0; for r := 0; r < nq; r += 2 { - s = q[r] + n; + s = q[r]+n; if s < 0 || s > ACTSIZE { continue nextn; } if amem[s] == 0 { flag++; - } else - if amem[s] != q[r+1] { + } else if amem[s] != q[r+1] { continue nextn; } } // check the position equals another only if the states are identical - for j:=0; j 1 { fmt.Fprintf(ftable, "State %v: entry at" - "%v equals state %v\n", i, n, j); + "%v equals state %v\n", + i, n, j); } return; } @@ -2873,7 +2783,7 @@ stin(i int) } for r := 0; r < nq; r += 2 { - s = q[r] + n; + s = q[r]+n; if s > maxa { maxa = s; } @@ -2895,10 +2805,8 @@ stin(i int) // this version is for limbo // write out the optimized parser // -func -aoutput() -{ - fmt.Fprintf(ftable, "const\tYYLAST\t= %v\n",maxa+1); +func aoutput() { + fmt.Fprintf(ftable, "const\tYYLAST\t= %v\n", maxa+1); arout("YYACT", amem, maxa+1); arout("YYPACT", indgo, nstate); arout("YYPGO", pgo, nnonter+1); @@ -2907,9 +2815,7 @@ aoutput() // // put out other arrays, copy the parsers // -func -others() -{ +func others() { var i, j int; arout("YYR1", levprd, nprod); @@ -2918,19 +2824,19 @@ others() // //yyr2 is the number of rules for each production // - for i=1; i= 0 && j < 256 { if temp1[j] != 0 { @@ -2965,7 +2871,7 @@ others() // table 2 has PRIVATE-PRIVATE+256 aryfil(temp1, 256, 0); c = 0; - for i=1; i<=ntokens; i++ { + for i = 1; i <= ntokens; i++ { j = tokset[i].value - PRIVATE; if j >= 0 && j < 256 { if temp1[j] != 0 { @@ -2984,7 +2890,7 @@ others() // table 3 has everything else fmt.Fprintf(ftable, "var\tYYTOK3\t= []int {\n"); c = 0; - for i=1; i<=ntokens; i++ { + for i = 1; i <= ntokens; i++ { j = tokset[i].value; if j >= 0 && j < 256 { continue; @@ -3012,9 +2918,7 @@ others() fmt.Fprintf(ftable, "%v", yaccpar); } -func -arout(s string, v []int, n int) -{ +func arout(s string, v []int, n int) { fmt.Fprintf(ftable, "var\t%v\t= []int {\n", s); for i := 0; i < n; i++ { if i%10 == 0 { @@ -3029,10 +2933,8 @@ arout(s string, v []int, n int) // // output the summary on y.output // -func -summary() -{ - if(foutput != nil) { +func summary() { + if foutput != nil { 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 shift/reduce, %v reduce/reduce conflicts reported\n", zzsrconf, zzrrconf); @@ -3061,9 +2963,7 @@ summary() // // write optimizer summary // -func -osummary() -{ +func osummary() { if foutput == nil { return; } @@ -3082,43 +2982,33 @@ osummary() // // copies and protects "'s in q // -func -chcopy(q string) string -{ +func chcopy(q string) string { s := ""; i := 0; j := 0; for i = 0; i < len(q); i++ { if q[i] == '"' { - s += q[j:i] + "\\"; + s += q[j:i]+"\\"; j = i; } } - return s + q[j:i]; + return s+q[j:i]; } -func -usage() -{ +func usage() { fmt.Fprintf(stderr, "usage: gacc [-o output] [-v parsetable] input\n"); exit(1); } -func -bitset(set Lkset, bit int) int -{ - return set[bit>>5] & (1<>5]&(1<>5] |= (1<= '0' && c <= '9'; } -func -isword(c int) bool -{ +func isword(c int) bool { return c >= 0xa0 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); } -func -mktemp(t string) string -{ +func mktemp(t string) string { return t; } @@ -3184,14 +3064,12 @@ mktemp(t string) string // return 1 if 2 arrays are equal // return 0 if not equal // -func -aryeq(a []int, b[]int) int -{ +func aryeq(a []int, b []int) int { n := len(a); if len(b) != n { return 0; } - for ll:=0; ll