Upgrade to Pro — share decks privately, control downloads, hide ads and more …

LRパーサーはいいぞ

Avatar for ydah ydah
May 11, 2025

 LRパーサーはいいぞ

Kernel/VM探検隊@関西 11回目「LRパーサーはいいぞ」の発表スライド
https://kernelvm.connpass.com/event/347564/

Avatar for ydah

ydah

May 11, 2025
Tweet

More Decks by ydah

Other Decks in Programming

Transcript

  1. 1BSTFSͱ-FYFS ࣈ ۟ ղ ੳ ث ߏ จ ղ ੳ

    ث จࣈྻ όΠτྻ τʔΫϯྻ ந৅ߏจ໦ "45
  2. 1BSTFSͱ-FYFS ࣈ ۟ ղ ੳ ث ߏ จ ղ ੳ

    ث จࣈྻ όΠτྻ τʔΫϯྻ ந৅ߏจ໦ "45
  3. -FYFS ॲཧܥ͔ΒݟΔͱόΠτྻ จࣈͷฒͼΛղੳ͠ɺҙຯͷ͋Δ࠷খͷ୯ҐʢτʔΫϯʣʹ෼ղ͢Δ 64 65 66 20 6d 65 74

    68 6f 64 5f 6e 61 6d 65 28 70 61 72 61 6d 29 0a 20 20 70 75 74 73 20 70 61 72 61 6d 0a 65 6e 64 def method_name param
  4. 1BSTFSͱ-FYFS ࣈ ۟ ղ ੳ ث ߏ จ ղ ੳ

    ث จࣈྻ όΠτྻ τʔΫϯྻ ந৅ߏจ໦ "45
  5. $PNNBOE-JOF*OUFSGBDF -SBNBͷίϚϯυ࣮ߦΛ࢘Δத৺తͳΫϥε ೖྗͱͳΔจ๏ϑΝΠϧ΍֤छΦϓγϣϯΛड͚औͬͯɺ1BSTFSੜ੒ϓϩ ηεશମͷΦʔέετϨʔγϣϯΛߦ͏ % lrama - h Lrama is

    LALR (1) parser generator written by Ruby. Usage: lrama [options] FILE STDIN mode: lrama [options] - FILE read grammar from STDIN Tuning the Parser: -S, - - skeleton=FILE specify the skeleton to use - t, - - debug display debugging outputs of internal parser same as '-Dparse.trace' - - locations enable location support -D, - - def i ne=NAME[=VALUE] similar to '%def i ne NAME VALUE'
  6. $PEF(FOFSBUPS εςʔτϚγϯͱߏจղੳදΛݩʹ࣮ߦՄೳͳ1BSTFSͷίʔυΛੜ੒͢Δ 5FNQMBUFʹ஋ΛຒΊࠐΜͰ͍͘ SVCZFSCͱ͍͏ςϯϓϨʔτγεςϜΛ࢖ͬͯຒΊࠐΉ / * - - - -

    - - - - - - . | yyparse. | ` - - - - - - - - - - * / int yyparse (<%= output.parse_param %>) { <%# b4_declare_scanner_communication_variables -%> / * Lookahead token kind. * / int yychar; / * The semantic value of the lookahead symbol. * / / * Default value used for initialization, for pacifying older GCCs or non-GCC compilers. * / #ifdef _ _ cplusplus static const YYSTYPE yyval_default = {}; (void) yyval_default; #else YY_INITIAL_VALUE (static const YYSTYPE yyval_default;)
  7. ࣅͨΑ͏ͳߏ଄͸Α͋͘Δ stmt : keyword_alias f i tem . . .

    | lhs '=' lex_ctxt mrhs { $$ = node_assign(p, $1, $4, $3, &@$); / * % ripper: assign!($ : 1, $ : 4) % * / } command_asgn : lhs '=' lex_ctxt command_rhs { $$ = node_assign(p, $1, $4, $3, &@$); / * % ripper: assign!($ : 1, $ : 4) % * / } arg : lhs '=' lex_ctxt arg_rhs { $$ = node_assign(p, $1, $4, $3, &@$); / * % ripper: assign!($ : 1, $ : 4) % * / }
  8. ࣅͨΑ͏ͳߏ଄͸Α͋͘Δ stmt : keyword_alias f i tem . . .

    | lhs '=' lex_ctxt mrhs { $$ = node_assign(p, $1, $4, $3, &@$); / * % ripper: assign!($ : 1, $ : 4) % * / } command_asgn : lhs '=' lex_ctxt command_rhs { $$ = node_assign(p, $1, $4, $3, &@$); / * % ripper: assign!($ : 1, $ : 4) % * / } arg : lhs '=' lex_ctxt arg_rhs { $$ = node_assign(p, $1, $4, $3, &@$); / * % ripper: assign!($ : 1, $ : 4) % * / }
  9. ߏ଄Λநग़͢Δ͜ͱ͕Ͱ͖Δ %rule asgn(rhs) <node> : lhs '=' lex_ctxt rhs {

    $$ = node_assign(p, $1, $4, $3, &@$); / * % ripper: assign!($ : 1, $ : 4) % * / } ; stmt : keyword_alias f i tem . . . | asgn(rhs) ; arg : asgn(arg_rhs) ; command_asgn : asgn(command_rhs) ;
  10. ύϥϝʔλͱͯ͠5PLFOΛ౉͢͜ͱ͕Ͱ͖Δ %rule asgn(rhs) <node> : lhs '=' lex_ctxt rhs {

    $$ = node_assign(p, $1, $4, $3, &@$); / * % ripper: assign!($ : 1, $ : 4) % * / } ; stmt : keyword_alias f i tem . . . | asgn(rhs) ; arg : asgn(arg_rhs) ; command_asgn : asgn(command_rhs) ;
  11. ౉͞Εͨ5PLFOΛӈลʹల։͢Δ %rule asgn(rhs) <node> : lhs '=' lex_ctxt rhs {

    $$ = node_assign(p, $1, $4, $3, &@$); / * % ripper: assign!($ : 1, $ : 4) % * / } ; stmt : keyword_alias f i tem . . . | asgn(rhs) ; arg : asgn(arg_rhs) ; command_asgn : asgn(command_rhs) ;
  12. ౉͞Εͨ5PLFOΛӈลʹల։͢Δ %rule asgn(rhs) <node> : lhs '=' lex_ctxt rhsarg_rhs {

    $$ = node_assign(p, $1, $4, $3, &@$); / * % ripper: assign!($ : 1, $ : 4) % * / } ; stmt : keyword_alias f i tem . . . | asgn(rhs) ; arg : asgn(arg_rhs) ; command_asgn : asgn(command_rhs) ;
  13. ౉͞Εͨ5PLFOΛӈลʹల։͢Δ %rule asgn(rhs) <node> : lhs '=' lex_ctxt rhscommand_rhs {

    $$ = node_assign(p, $1, $4, $3, &@$); / * % ripper: assign!($ : 1, $ : 4) % * / } ; stmt : keyword_alias f i tem . . . | asgn(rhs) ; arg : asgn(arg_rhs) ; command_asgn : asgn(command_rhs) ;
  14. จ๏ͷߏ଄Λ͍͍ײ͡ʹݟ͍ͨ brace_block : '{' brace_body '}' { $$ = $2;

    set_embraced_location($$, &@1, &@3); / * % ripper: $ : 2 % * / } | k_do do_body k_end { $$ = $2; set_embraced_location($$, &@1, &@3); / * % ripper: $ : 2 % * / } ;
  15. จ๏ͷߏ଄ͱ"DUJPO͕ࠞࡏ brace_block : '{' brace_body '}' { $$ = $2;

    set_embraced_location($$, &@1, &@3); / * % ripper: $ : 2 % * / } | k_do do_body k_end { $$ = $2; set_embraced_location($$, &@1, &@3); / * % ripper: $ : 2 % * / } ;
  16. จ๏ͷߏ଄ͱ"DUJPO͕ࠞࡏ brace_block : '{' brace_body '}' { $$ = $2;

    set_embraced_location($$, &@1, &@3); / * % ripper: $ : 2 % * / } | k_do do_body k_end { $$ = $2; set_embraced_location($$, &@1, &@3); / * % ripper: $ : 2 % * / } ;