HyperDbg Debugger
Loading...
Searching...
No Matches
lalr1_parser Namespace Reference

Classes

class  LALR1Parser
 

Functions

 describe_grammar (gr)
 
 describe_parsing_table (table)
 
 get_grammar (parser)
 
 main ()
 

Detailed Description

 * @file lalr1_parse_table_generator.py
 * @author M.H. Gholamrezei (mh@hyperdbg.org)
 * @brief Script engine LALR(1) Parse table generator 
 * @details This program reads grammar from Boolean_Expression_Grammar.txt file 
 *          placed in the same directory of the program 
 *          and creates parse_table.h and parse_table.c which is 
 *          used by the parser of script engine. 
 * @version 0.1
 * @date 2021-02-07
 *
 * @copyright This project is released under the GNU Public License v3.

Function Documentation

◆ describe_grammar()

lalr1_parser.describe_grammar ( gr)
602def describe_grammar(gr):
603 return '\n'.join([
604 'Indexed grammar rules (%d in total):' % len(gr.productions),
605 str(gr) + '\n',
606 'Grammar non-terminals (%d in total):' % len(gr.nonterms),
607 '\n'.join('\t' + str(s) for s in gr.nonterms) + '\n',
608 'Grammar terminals (%d in total):' % len(gr.terminals),
609 '\n'.join('\t' + str(s) for s in gr.terminals)
610 ])
611
612

◆ describe_parsing_table()

lalr1_parser.describe_parsing_table ( table)
613def describe_parsing_table(table):
614 conflict_status = table.get_conflict_status()
615
616 def conflict_status_str(state_id):
617 has_sr_conflict = (conflict_status[state_id] == lalr_one.STATUS_SR_CONFLICT)
618 status_str = ('shift-reduce' if has_sr_conflict else 'reduce-reduce')
619 return 'State %d has a %s conflict' % (state_id, status_str)
620
621 return ''.join([
622 'PARSING TABLE SUMMARY\n',
623 'Is the given grammar LALR(1)? %s\n' % ('Yes' if table.is_lalr_one() else 'No'),
624 ''.join(conflict_status_str(sid) + '\n' for sid in range(table.n_states)
625 if conflict_status[sid] != lalr_one.STATUS_OK) + '\n',
626 table.stringify()
627 ])
628

◆ get_grammar()

lalr1_parser.get_grammar ( parser)
629def get_grammar(parser):
630 Rules = []
631 i = 0
632 preLhs = parser.LhsList[0]
633 RhsList = []
634 for Lhs in parser.LhsList:
635 Rhs = ""
636
637 for X in parser.RhsList[i]:
638 if parser.IsNoneTerminal(X):
639 Rhs += X
640 elif parser.IsSemanticRule(X):
641 continue
642 else:
643 Rhs += ("'" + X + "'")
644 Rhs += " "
645 if parser.RhsList[i][0] == 'eps':
646 Rhs = ""
647
648 if preLhs != Lhs:
649 Rules.append(NonTerminal(preLhs, RhsList))
650 RhsList = []
651
652
653 RhsList.append(Rhs)
654
655 i = i + 1
656 preLhs = Lhs
657
658 Rules.append(NonTerminal(preLhs, RhsList))
659
660 return Grammar(Rules)
661
662

◆ main()

lalr1_parser.main ( )
663def main():
664 print('Working on it...')
665 parser = LALR1Parser()
666 parser.Run()
667 gr = get_grammar(parser)
668 table = lalr_one.ParsingTable(gr)
669 print("I'm done.")
670
671 output_filename = 'parsing-table'
672
673 with open(output_filename + '.txt', 'w') as textfile:
674 textfile.write(describe_grammar(gr))
675 textfile.write('\n\n')
676 textfile.write(describe_parsing_table(table))
677
678 table.save_to_csv(output_filename + '.csv')
679 parser.Parse(table, ['(', '_hex', '&', 'id', '>', 'id', ')', '&', '(', 'id', '<', 'id',')', '$end'])
int main()
main function
Definition hyperdbg-app.cpp:113