Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

建议给Node类添加print函数 #1

Open
konyakest opened this issue Feb 4, 2023 · 0 comments
Open

建议给Node类添加print函数 #1

konyakest opened this issue Feb 4, 2023 · 0 comments

Comments

@konyakest
Copy link

调试时,直接输出Node很难读,例如:

import LIBLR

grammar = r'''
%token NUMBER


%left '+' '-'
%left '*' '/' '%'
%left '^'

expr:expr '^' expr             {pow}
    |expr '+' expr             {add}
    | expr '-' expr             {sub}
    | expr '*' expr             {mul}
    | expr '/' expr             {div}
    | '(' expr ')'              {get2}
    | '-' expr                  {negative}
    | '+' expr                  {positive}
    | NUMBER                    {getint}
    ;

@ignore [ \r\n\t]*
@match NUMBER \d+
'''

parser = LIBLR.create_parser(grammar)
a=parser('1^6*(1+3)/2')

print(a)

输出

Node(Symbol('expr'), [Node(Symbol('expr'), [Node(Symbol('expr'), [Node(Symbol('expr'), ['1']), '^', Node(Symbol('expr'), ['6'])]), '*', Node(Symbol('expr'), ['(', Node(Symbol('expr'), [Node(Symbol('expr'), ['1']), '+', Node(Symbol('expr'), ['3'])]), ')'])]), '/', Node(Symbol('expr'), ['2'])])

建议给Node类添加print函数:

#----------------------------------------------------------------------
# Node
#----------------------------------------------------------------------
def _printNode(x,prestr):
    print(prestr,end='')
    if type(x)==type(' '):
        print(x)
        return
    print(x.name)
    for i in x.child:
        _printNode(i,prestr+'| ')


class Node (object):
    
    def __init__ (self, name, child):
        self.name = name
        self.child = [n for n in child]

    def __repr__ (self):
        clsname = type(self).__name__
        return '%s(%r, %r)'%(clsname, self.name, self.child)

    def __str__ (self):
        return self.__repr__()
    
    def print(self):
        _printNode(self,'')

这样,输出信息就很清晰了

import LIBLR

grammar = r'''
%token NUMBER


%left '+' '-'
%left '*' '/' '%'
%left '^'

expr:expr '^' expr             {pow}
    |expr '+' expr             {add}
    | expr '-' expr             {sub}
    | expr '*' expr             {mul}
    | expr '/' expr             {div}
    | '(' expr ')'              {get2}
    | '-' expr                  {negative}
    | '+' expr                  {positive}
    | NUMBER                    {getint}
    ;

@ignore [ \r\n\t]*
@match NUMBER \d+
'''

parser = LIBLR.create_parser(grammar)
a=parser('1^6*(1+3)/2')

a.print()
expr
| expr
| | expr
| | | expr
| | | | 1
| | | ^
| | | expr
| | | | 6
| | *
| | expr
| | | (
| | | expr
| | | | expr
| | | | | 1
| | | | +
| | | | expr
| | | | | 3
| | | )
| /
| expr
| | 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant