Syntactic analysis with Gocc

91 views
Skip to first unread message

Abraham

unread,
Sep 8, 2020, 2:56:33 PM9/8/20
to golang-nuts
Hi, I am trying to implement a parser with Gocc. This is the file with the definition of the grammar.

/* *************************** LEXER **************************** */

!espacio_en_blanco:     '\t' | '\n' | '\r' | ' ' ;
!comentario:            '#' { . } '\n' ;

_letra:                 'A'-'Z' | 'a'-'z' | '_' ;
_digito:                '0'-'9' ;
_alfa:                  _letra | _digito ;

cadena:                 '"' {_alfa | ' ' | '!' | '.' } '"' ;
entero:                 '0' | '1'-'9' {_digito} ;

multi:                  '/' '*' ;

mkdisk:                 'm' 'k' 'd' 'i' 's' 'k' ;
rmdisk:                 'r' 'm' 'd' 'i' 's' 'k' ;
fdisk:                  'f' 'd' 'i' 's' 'k' ;
mount:                  'm' 'o' 'u' 'n' 't' ;
unmount:                'u' 'n' 'm' 'o' 'u' 'n' 't' ;
rep:                    'r' 'e' 'p' ;
 
size:                   's' 'i' 'z' 'e' ;
fit:                    'f' 'i' 't' ;
unit:                   'u' 'n' 'i' 't' ;
path:                   'p' 'a' 't' 'h' ;
type:                   't' 'y' 'p' 'e' ;
delete:                 'd' 'e' 'l' 'e' 't' 'e' ;
name:                   'n' 'a' 'm' 'e' ;
add:                    'a' 'd' 'd' ;
id:                     'i' 'd' ;

mbr:                    'm' 'b' 'r' ;
disk:                   'd' 'i' 's' 'k' ;

identificador:          _letra {_alfa} ;

gn:                     '-' ;
igl:                    '=' ;

/* *************************************************************** */

/* ************************* PARSER ************************** */

<<
    import(        
        "fmt"
    )
>>

Cmd:            Mkdisk <<  fmt.Println("Axioma") >> ;

Mkdisk:         mkdisk OpcMkdisk << fmt.Println("Instrucción reconocida.") >> ;

OpcMkdisk:      OpcMkdisk Opc << fmt.Println("Producción recursiva.") >>
            |   Opc << fmt.Println("Se ejecuta primero.") >> ;            

Opc:            Size
            |   Path
            |   Unit
            |   MLinea ;

Size:           gn size << fmt.Println("SIZE") >> ;            

Path:           gn path << fmt.Println("PATH") >> ;

Unit:           gn unit << fmt.Println("UNIT") >> ;

MLinea:         gn fit << fmt.Println("ML") >> ;

/* *************************************************************** */

The test method is as follows:

func main() {
texto := "mkdisk -size -path -unit -fit"
p := parser.NewParser()
s := lexer.NewLexer([]byte(texto))
res, err := p.Parse(s)
if err != nil {
fmt.Println("Se produjo un error.")
} else if res != nil {
fmt.Println("Se reconoció la instrucción.")
}
}

However the parse input, I am not able to correctly read the latest production of Opc (MLinea) and a syntax error. I think the definition is correct, but I don't get the desired result.
What am I doing wrong?
Thanks

Ian Lance Taylor

unread,
Sep 8, 2020, 5:45:05 PM9/8/20
to Abraham, golang-nuts
On Tue, Sep 8, 2020 at 11:56 AM Abraham <luisenr...@gmail.com> wrote:
>
> Hi, I am trying to implement a parser with Gocc. This is the file with the definition of the grammar.

...

> However the parse input, I am not able to correctly read the latest production of Opc (MLinea) and a syntax error. I think the definition is correct, but I don't get the desired result.
> What am I doing wrong?

You might need to reach out to the gocc maintainers directly.

Ian

LEN P

unread,
Sep 8, 2020, 6:15:30 PM9/8/20
to Ian Lance Taylor, golang-nuts
Thanks, was my mistake. 
Reply all
Reply to author
Forward
0 new messages