~ubuntu-branches/ubuntu/wily/styx/wily

« back to all changes in this revision

Viewing changes to libstyx/styx_gen.c

  • Committer: Package Import Robot
  • Author(s): Frederik Schüler
  • Date: 2013-12-28 18:20:37 UTC
  • mfrom: (0.1.5)
  • Revision ID: package-import@ubuntu.com-20131228182037-ihsf1de5y1oe24nf
Tags: 2.0.1-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
83
83
  MAP(PT_Term, PT_Term)    glo;         /* Global definitions             */
84
84
  MAP(symbol,PT_Term)      grp;         /* Scan groups                    */
85
85
  MAP(GLS_Tok,symbol)      grptok;      /* Scan group tokens              */
 
86
  MAP(symbol,int)          dyntok;      /* Dynamic token references       */
86
87
  MAP(symbol,symbol)       langrp;      /* Embedded language token group  */
87
88
  MAP(GLS_Tok,symbol)      tokgrp;      /* Token --> group to activate    */
88
89
  MAP(PT_Term,symbol)      ebnfmbr;     /* EBNF production member         */
203
204
    MAP_freeMap(styctx->grptok);
204
205
    styctx->grptok = (MAP(_,_))NULL;
205
206
  }
 
207
  if( styctx->dyntok != (MAP(_,_))NULL ) 
 
208
  { 
 
209
    MAP_freeMap(styctx->dyntok);
 
210
    styctx->dyntok = (MAP(_,_))NULL;
 
211
  }
206
212
  if( styctx->langrp != (MAP(_,_))NULL ) 
207
213
  { 
208
214
    MAP_freeMap(styctx->langrp);
326
332
  styctx->extctx      = (Any_T)NULL;
327
333
  styctx->grp         = (MAP(_,_))NULL;
328
334
  styctx->grptok      = (MAP(_,_))NULL;
 
335
  styctx->dyntok      = (MAP(_,_))NULL;
329
336
  styctx->langrp      = (MAP(_,_))NULL;
330
337
  styctx->tokgrp      = (MAP(_,_))NULL;
331
338
  styctx->ebnfmbr     = (MAP(_,_))NULL;
577
584
 
578
585
static c_bool qlx_quotient(Styx_T styctx, GLS_Tok id)
579
586
{ styxExp exp;
580
 
  styxQlxDfn_defn((styxQlxDfn)getDfn(styctx,id,C_True),_,_,_,_,_,&exp);
 
587
  styxQlxDfn qd = (styxQlxDfn)getDfn(styctx,id,C_True);
 
588
  if( !styxQlxDfn_defn(qd,_,_,_,_,_,&exp) )
 
589
    return C_False;
581
590
  return styxExp_quot(exp,_,_);
582
591
}
583
592
 
584
593
static c_bool qlx_dyck(Styx_T styctx, GLS_Tok id)
585
594
{ styxExp exp;
586
 
  styxQlxDfn_defn((styxQlxDfn)getDfn(styctx,id,C_True),_,_,_,_,_,&exp);
 
595
  styxQlxDfn qd = (styxQlxDfn)getDfn(styctx,id,C_True);
 
596
  if( !styxQlxDfn_defn(qd,_,_,_,_,_,&exp) )
 
597
    return C_False;
587
598
  if( styxExp_quot(exp,&exp,_) )
588
599
    /* noop */;
589
600
  return styxExp_dyck(exp,_,_,_);
591
602
 
592
603
static c_bool qlx_pattern(Styx_T styctx, GLS_Tok id)
593
604
{ styxExp exp;
594
 
  styxQlxDfn_defn((styxQlxDfn)getDfn(styctx,id,C_True),_,_,_,_,_,&exp);
 
605
  styxQlxDfn qd = (styxQlxDfn)getDfn(styctx,id,C_True);
 
606
  if( !styxQlxDfn_defn(qd,_,_,_,_,_,&exp) )
 
607
    return C_False;
595
608
  if( styxExp_quot(exp,&exp,_) )
596
609
    /* noop */;
597
610
  return styxExp_spat(exp,_,_,_) || styxExp_epat(exp,_,_,_);
599
612
 
600
613
static c_bool qlx_ica(Styx_T styctx, GLS_Tok id)
601
614
{ styxQlxOpt opt;
602
 
  styxQlxDfn_defn((styxQlxDfn)getDfn(styctx,id,C_True),_,&opt,_,_,_,_);
 
615
  styxQlxDfn qd = (styxQlxDfn)getDfn(styctx,id,C_True);
 
616
  if( !styxQlxDfn_defn(qd,_,&opt,_,_,_,_) )
 
617
    return C_False;
603
618
  return styxQlxOpt_ignca(opt);
604
619
}
605
620
 
606
621
static void Pass1(Styx_T styctx, styxSource s)
607
622
/* Collecting definitions and scoping rules */
608
 
{ Styx_T lanctx, rootctx; bool startflg = False; int indcnt = 0, rc;
 
623
{ Styx_T lanctx, rootctx; bool startflg = False; 
 
624
  int indcnt = 0, dyncnt = 0, rc;
609
625
  MAP(PT_Term,NULL) pro = MAP_newMap(styctx->gloty); /* Productions */
610
626
  PT_Itr it; styxOptCfg optcfg; GLS_Lst(styxDfn) dfns;
611
627
  rootctx = RootCtx(styctx);
617
633
  { PT_Term t = PT_termIT(it), st = (PT_Term)NULL;
618
634
    if (PT_stateIT(it) == PT_POST)
619
635
    { styxQlxDfn qd; styxDfn pd; styxPrd p; GLS_Tok id, sgid, dgid;
 
636
      styxMbr mbr;
620
637
      /* Scope(Source): all identifiers in QLX definitions */
621
638
      if (styx_QlxDfn(t,&qd) )
622
639
      { styxQlxOpt o; styxQlxCat c; styxQlxGrp gsrc, gdst; styxExp e, e1;
623
 
        if( styxQlxDfn_igrp(qd,&id) || styxQlxDfn_xgrp(qd,&id) ||
 
640
        if( styxQlxDfn_igrp(qd,&id) || styxQlxDfn_xgrp(qd,&id) || 
 
641
            styxQlxDfn_tgrp(qd,&id) ||
624
642
            styxQlxDfn_mgrp(qd,&id,_) )
625
643
        { GLS_Lst(GLS_Tok) ids, lst;
626
644
          if (MAP_defined(styctx->grp,GLS_Tok_symbol(id)) ||
644
662
                if( !MAP_defined(styctx->grp,GLS_Tok_symbol(grpid)) )
645
663
                  PT_diag_err
646
664
                  (
647
 
                    grpid,styctx->diag,"identifier '%s' undefined",
 
665
                    grpid,styctx->diag,"undefined identifier '%s'",
648
666
                    GLS_Tok_string(grpid)
649
667
                  );
650
668
                else
833
851
          if( indcnt > 1 )
834
852
            PT_diag_err(o,styctx->diag,"too many (de)indent tokens");
835
853
        }
 
854
        else
 
855
        if ( styxQlxDfn_defd(qd,&id) )
 
856
        {
 
857
          if( MAP_defined(styctx->glo,id))
 
858
            PT_diag_err
 
859
            (
 
860
              id,styctx->diag,"identifier '%s' already defined",
 
861
              GLS_Tok_string(id)
 
862
            );
 
863
          else 
 
864
          {
 
865
            MAP_dfndom(styctx->glo,id,qd);
 
866
            MAP_dfndom(styctx->dyntok,GLS_Tok_symbol(id),0);
 
867
          }
 
868
        }
836
869
      }
837
870
 
838
871
      /* Scope(Source): all identifiers in CFG definitions */
861
894
      /* Scope(Definition): all production identifiers */
862
895
      if (styx_Prd(t,&p))
863
896
      { 
 
897
        dyncnt = 0;
864
898
        if ( styxPrd_prod(p,_,&id,_) )
865
899
        { 
866
900
          if (MAP_defined(pro,id))
872
906
          else MAP_dfndom(pro,id,NULL);
873
907
        }
874
908
      }
 
909
 
 
910
      if (styx_Mbr(t,&mbr) )
 
911
      {
 
912
        if( styxMbr_dtok(mbr,_,_) )
 
913
          ++dyncnt;
 
914
        if( dyncnt == 2 )
 
915
          PT_diag_err
 
916
          (
 
917
            mbr,styctx->diag,"production with more than one dynamic token members"
 
918
          );
 
919
      }
875
920
    }
876
921
  }
877
 
  if( ! startflg && styxOptCfg_cfg(optcfg,&dfns) && ! GLS_EMPTY(dfns) ) 
 
922
  if( ! startflg && styxOptCfg_cfg(optcfg,&dfns,_) && ! GLS_EMPTY(dfns) ) 
878
923
    PT_diag_err(dfns,styctx->diag,"missing start production");
879
924
  MAP_freeMap(pro);
880
925
}
881
926
 
882
 
static void Pass2_checkMbrIde(Styx_T styctx, GLS_Tok id, GLS_Lst(styxQlxDfn) qlxdfns)
883
 
/* check wether production member 'id' is a defined token or nonterminal */
 
927
static int Pass2_checkMbrIde(Styx_T styctx, GLS_Tok id, GLS_Lst(styxQlxDfn) qlxdfns)
 
928
/* check wether production or conflict member 'id' is a defined token or nonterminal 
 
929
   rc = -1 (unknown), 0 (token), 1 (nonterminal), 2 (embedded language token)
 
930
*/
884
931
{
885
932
  Styx_T rootctx = RootCtx(styctx);
886
933
  if (!MAP_defined(styctx->glo,id) &&
887
934
      !MAP_defined(rootctx->embed,GLS_Tok_symbol(id)))
888
935
  {
889
 
    if( !GLS_EMPTY(qlxdfns) )
890
 
      PT_diag_err
891
 
      (
892
 
        id,styctx->diag,"identifier '%s' undefined",GLS_Tok_string(id)
893
 
      );
 
936
    if( GLS_EMPTY(qlxdfns) )
 
937
    {
 
938
      return 0;
 
939
    }
894
940
  }
895
941
  else
896
942
  if (MAP_defined(styctx->glo,id))
897
943
  { PT_Term def = MAP_apply(PT_Term,styctx->glo,id); 
898
944
    styxQlxDfn qd; styxQlxCat qc;
899
 
    if( ! ( styx_Dfn(def,_)                     ||
900
 
            ( styx_QlxDfn(def,&qd)              &&
901
 
              styxQlxDfn_defn(qd,&qc,_,_,_,_,_) &&
902
 
              (styxQlxCat_tokC(qc) || styxQlxCat_lanC(qc)) ) ) )
 
945
    if( ! ( styx_Dfn(def,_) ||
 
946
            ( styx_QlxDfn(def,&qd) &&
 
947
              ( styxQlxDfn_defd(qd,_) ||
 
948
                styxQlxDfn_defn(qd,&qc,_,_,_,_,_) &&
 
949
                (styxQlxCat_tokC(qc) || styxQlxCat_lanC(qc)) ) ) ) )
 
950
    {
903
951
      PT_diag_err
904
952
      (
905
 
        id,styctx->diag,"'%s' is no grammar symbol",GLS_Tok_string(id)
 
953
        id,styctx->diag,"invalid production member symbol '%s'",GLS_Tok_string(id)
906
954
      );
907
 
  }
 
955
      return -1;
 
956
    }
 
957
    return styx_Dfn(def,_) ? 1 : 0;
 
958
  }
 
959
  else
 
960
  if( MAP_defined(styctx->grp,GLS_Tok_symbol(id)) )
 
961
  {
 
962
    return 2;
 
963
  }
 
964
 
 
965
  PT_diag_err
 
966
  (
 
967
    id,styctx->diag,"undefined grammar symbol '%s'",GLS_Tok_string(id)
 
968
  );
 
969
  return -1; 
908
970
}
909
971
 
910
972
static void Pass2_checkMbr(Styx_T styctx, styxMbr m, GLS_Lst(styxQlxDfn) qlxdfns)
911
973
/* check production member 'm' (Pass2) */
912
 
{ GLS_Tok id;
 
974
{ GLS_Tok id, did; int rc, flg;
913
975
  if ( styxMbr_ntm(m,&id) )
914
976
  { 
915
 
    Pass2_checkMbrIde(styctx,id,qlxdfns);
 
977
    rc = Pass2_checkMbrIde(styctx,id,qlxdfns);
 
978
    if( rc == 0 && MAP_defined(styctx->dyntok,GLS_Tok_symbol(id)) )
 
979
    {
 
980
      flg = MAP_apply(int,styctx->dyntok,GLS_Tok_symbol(id));
 
981
      flg |= 2;
 
982
      MAP_upddom(styctx->dyntok,GLS_Tok_symbol(id),(long)flg);
 
983
    }
 
984
  }
 
985
  else
 
986
  if ( styxMbr_dtok(m,&id,&did) )
 
987
  { 
 
988
    rc = Pass2_checkMbrIde(styctx,id,qlxdfns);
 
989
    if( rc == 1 || rc == 2 ||
 
990
        MAP_defined(styctx->dyntok,GLS_Tok_symbol(id)) )
 
991
    {
 
992
      PT_diag_err
 
993
      (
 
994
        id,styctx->diag,"expected token '%s'",GLS_Tok_string(id)
 
995
      );
 
996
    }
 
997
    if( !MAP_defined(styctx->dyntok,GLS_Tok_symbol(did)) )
 
998
    {
 
999
      PT_diag_err
 
1000
      (
 
1001
        did,styctx->diag,"undefined dynamic token '%s'",GLS_Tok_string(did)
 
1002
      );
 
1003
    }
 
1004
    else
 
1005
    {
 
1006
      flg = MAP_apply(int,styctx->dyntok,GLS_Tok_symbol(did));
 
1007
      flg |= 1;
 
1008
      MAP_upddom(styctx->dyntok,GLS_Tok_symbol(did),(long)flg);
 
1009
    }
916
1010
  }
917
1011
  else
918
1012
  { styxMbr em;
924
1018
  }
925
1019
}
926
1020
 
 
1021
static void Pass2_checkConflict
 
1022
            (
 
1023
              Styx_T styctx, styxConflict conflict,
 
1024
              GLS_Lst(styxQlxDfn) qlxdfns
 
1025
            )
 
1026
/* check wether conflict token, state and rules refer to defined
 
1027
   token, nonterminals and productions.
 
1028
*/
 
1029
{ styxState state; styxToken token; GLS_Lst(styxRule) rules, itr;
 
1030
  if( styxConflict_defn(conflict,&state,&token,&rules) )
 
1031
  { GLS_Tok id1, id2, pid;
 
1032
    int rc;
 
1033
    if( styxState_ide(state,&id1) )
 
1034
    { 
 
1035
      rc = Pass2_checkMbrIde(styctx,id1,qlxdfns);
 
1036
    }
 
1037
    if( styxToken_ide(token,&id1) )
 
1038
    { 
 
1039
      rc = Pass2_checkMbrIde(styctx,id1,qlxdfns);
 
1040
      if( rc == 1 ) /* nonterminal */
 
1041
      {
 
1042
        PT_diag_err
 
1043
        (
 
1044
          id1,styctx->diag,"'%s' must be a token",GLS_Tok_string(id1)
 
1045
        );
 
1046
      }
 
1047
    }
 
1048
    GLS_FORALL(itr,rules)
 
1049
    { 
 
1050
      styxRule rule = GLS_FIRST(styxRule,itr);
 
1051
      if( styxRule_red(rule,&id1,&id2) )
 
1052
      {
 
1053
        rc = Pass2_checkMbrIde(styctx,id1,qlxdfns);
 
1054
        if( rc == 0 || rc == 2 ) /* token */
 
1055
        {
 
1056
          PT_diag_err
 
1057
          (
 
1058
            id1,styctx->diag,"'%s' must be a nonterminal",GLS_Tok_string(id1)
 
1059
          );
 
1060
        }
 
1061
        else
 
1062
        if( rc == 1 ) /* nonterminal */
 
1063
        { PT_Term def = MAP_apply(PT_Term,styctx->glo,id1); 
 
1064
          styxDfn pd; GLS_Lst(styxPrd) prds, prditr;
 
1065
          if( styx_Dfn(def,&pd) && styxDfn_defn(pd,_,_,_,&prds) )
 
1066
          {
 
1067
            c_bool found = C_False;
 
1068
            GLS_FORALL(prditr,prds)
 
1069
            { styxPrd p = GLS_FIRST(styxPrd,prditr);
 
1070
              if( styxPrd_prod(p,_,&pid,_) && 
 
1071
                  GLS_Tok_symbol(pid) == GLS_Tok_symbol(id2) ); 
 
1072
              {
 
1073
                found = C_True; break;
 
1074
              }
 
1075
            }
 
1076
            if( !found )
 
1077
            {
 
1078
              PT_diag_err
 
1079
              (
 
1080
                id2,styctx->diag,"'%s' must be a production name",GLS_Tok_string(id2)
 
1081
              );
 
1082
            }
 
1083
          }
 
1084
        }
 
1085
      }
 
1086
    }
 
1087
  }
 
1088
}
 
1089
 
927
1090
static void Pass2(Styx_T styctx, styxSource s)
928
1091
/* References */
929
1092
{ PT_Itr it; Styx_T rootctx = RootCtx(styctx), tokctx;
951
1114
    }
952
1115
    else
953
1116
    if (PT_stateIT(it) == PT_POST)
954
 
    { styxMbr m; 
 
1117
    { styxMbr m; styxConflict cr;
955
1118
      if (styx_Exp(t,&exp) )
956
1119
      {
957
1120
        if ( styxExp_ident(exp,&id) )
958
 
        {
 
1121
        { styxQlxDfn qd;
959
1122
          tokctx = styctx;
960
1123
          if( MAP_defined(lanexp,exp) )
961
1124
            tokctx = MAP_apply(Styx_T,lanexp,exp);
962
1125
          if (!MAP_defined(tokctx->glo,id))
963
1126
            PT_diag_err
964
1127
            (
965
 
              id,styctx->diag,"identifier '%s' undefined",GLS_Tok_string(id)
 
1128
              id,styctx->diag,"undefined identifier '%s'",GLS_Tok_string(id)
966
1129
            );
967
1130
          else
968
 
            if (!styx_QlxDfn(MAP_apply(PT_Term,tokctx->glo,id),_))
 
1131
            if (!styx_QlxDfn(MAP_apply(PT_Term,tokctx->glo,id),&qd))
969
1132
              PT_diag_err
970
1133
              (
971
 
                id,styctx->diag,"'%s' is no QLX-identifier",
972
 
                GLS_Tok_string(id)
 
1134
                id,styctx->diag,"undefined QLX-identifier '%s'", GLS_Tok_string(id)
973
1135
              );
974
1136
          else
975
 
            if( qlx_usage(tokctx,id) == UsageLan || 
 
1137
            if( styxQlxDfn_defd(qd,_) ||
 
1138
                qlx_usage(tokctx,id) == UsageLan || 
976
1139
                qlx_quotient(tokctx,id) || qlx_dyck(tokctx,id) 
977
1140
                /*|| qlx_pattern(tokctx,id)*/ )
978
1141
              PT_diag_err
986
1149
      { 
987
1150
        Pass2_checkMbr(styctx,m,qlxdfns);
988
1151
      }
 
1152
      else
 
1153
      if( styx_Conflict(t,&cr) )
 
1154
      {
 
1155
        Pass2_checkConflict(styctx,cr,qlxdfns);
 
1156
      }
989
1157
    }
990
1158
  }
991
1159
  MAP_freeMap(lanexp);
1740
1908
        ftoks = (MAP(_,_))NULL;
1741
1909
        if( parctx == styctx )
1742
1910
        {
1743
 
          grpid = StrCopy(symbolToString(grpsym));
 
1911
          if( styxQlxDfn_tgrp(d,_) )
 
1912
            grpid = Str_printf("tok_%s",symbolToString(grpsym));
 
1913
          else
 
1914
            grpid = StrCopy(symbolToString(grpsym));
1744
1915
        }
1745
1916
        else
1746
1917
        {
1747
 
          grpid = Str_printf
1748
 
                  (
1749
 
                    "%s__%s",styctx->language,symbolToString(grpsym)
1750
 
                  );
 
1918
          if( styxQlxDfn_tgrp(d,_) )
 
1919
            grpid = Str_printf
 
1920
                    (
 
1921
                      "%s__tok_%s",styctx->language,symbolToString(grpsym)
 
1922
                    );
 
1923
          else
 
1924
            grpid = Str_printf
 
1925
                    (
 
1926
                      "%s__%s",styctx->language,symbolToString(grpsym)
 
1927
                    );
1751
1928
          lansym = stringToSymbol(styctx->language);
1752
1929
          if( MAP_defined(g_ftoks,lansym) )
1753
1930
            ftoks = MAP_apply(MAP(_,_),g_ftoks,lansym);
1788
1965
  MAP_forItrAsg(id,rng,itr,styctx->tokgrp)
1789
1966
  {
1790
1967
    grpsym = MAP_apply(symbol,styctx->grptok,id);
 
1968
    if( MAP_defined(styctx->grp,grpsym) )
 
1969
      d = MAP_apply(styxQlxDfn,styctx->grp,grpsym);
 
1970
    else d = (styxQlxDfn)NULL;
1791
1971
    if( grpsym == InitialGroup )
1792
1972
      grpid = StrCopy(igrpid);
1793
1973
    else
1794
1974
    if( parctx == styctx )
1795
 
      grpid = StrCopy(symbolToString(grpsym));
 
1975
    {
 
1976
      if( d != (styxQlxDfn)NULL && styxQlxDfn_tgrp(d,_) )
 
1977
        grpid = Str_printf("tok_%s",symbolToString(grpsym));
 
1978
      else
 
1979
        grpid = StrCopy(symbolToString(grpsym));
 
1980
    }
1796
1981
    else
1797
 
      grpid = Str_printf
1798
 
              (
1799
 
                "%s__%s",styctx->language,symbolToString(grpsym)
1800
 
              );
 
1982
    {
 
1983
      if( d != (styxQlxDfn)NULL && styxQlxDfn_tgrp(d,_) )
 
1984
        grpid = Str_printf
 
1985
                (
 
1986
                  "%s__tok_%s",styctx->language,symbolToString(grpsym)
 
1987
                );
 
1988
      else
 
1989
        grpid = Str_printf
 
1990
                (
 
1991
                  "%s__%s",styctx->language,symbolToString(grpsym)
 
1992
                );
 
1993
    }
 
1994
    if( MAP_defined(styctx->grp,rng) )
 
1995
      d = MAP_apply(styxQlxDfn,styctx->grp,rng);
1801
1996
    if( rng == InitialGroup )
1802
1997
      sgrpid = StrCopy(igrpid);
1803
1998
    else
1812
2007
    }
1813
2008
    else
1814
2009
    if( parctx == styctx )
1815
 
      sgrpid = StrCopy(symbolToString(rng));
 
2010
    {
 
2011
      if( d != (styxQlxDfn)NULL && styxQlxDfn_tgrp(d,_) )
 
2012
        sgrpid = Str_printf("tok_%s",symbolToString(rng));
 
2013
      else
 
2014
        sgrpid = StrCopy(symbolToString(rng));
 
2015
    }
1816
2016
    else
1817
 
      sgrpid = Str_printf
1818
 
               (
1819
 
                 "%s__%s",styctx->language,symbolToString(rng)
1820
 
               );
 
2017
    {
 
2018
      if( d != (styxQlxDfn)NULL && styxQlxDfn_tgrp(d,_) )
 
2019
        sgrpid = Str_printf
 
2020
                 (
 
2021
                   "%s__tok_%s",styctx->language,symbolToString(rng)
 
2022
                 );
 
2023
      else
 
2024
        sgrpid = Str_printf
 
2025
                 (
 
2026
                   "%s__%s",styctx->language,symbolToString(rng)
 
2027
                 );
 
2028
    }
1821
2029
    if( !MAP_defined(styctx->metagrp,grpsym) )
1822
2030
    {
1823
2031
      if( MAP_defined(patids,id) )
1895
2103
/* checking keywords */
1896
2104
 
1897
2105
static void Pass3_checkMbrSeq(Styx_T styctx, GLS_Tok seq, GLS_Lst(styxQlxDfn) qlxdfns)
1898
 
/* check wether production keyword 'seq' is well defined */
 
2106
/* check wether production or conflict keyword 'seq' is well defined */
1899
2107
{
1900
2108
  c_bool u; int t; StdCPtr s = TRANS_Seq(seq,&u);
1901
2109
  /*FIXME: comments are not allowed, too*/
1976
2184
  }
1977
2185
}
1978
2186
 
 
2187
static void Pass3_checkConflict
 
2188
            (
 
2189
              Styx_T styctx, styxConflict conflict,
 
2190
              GLS_Lst(styxQlxDfn) qlxdfns
 
2191
            )
 
2192
/* check wether conflict token, state and rules refer to defined
 
2193
   token, nonterminals and productions.
 
2194
*/
 
2195
{ styxState state; styxToken token;
 
2196
  if( styxConflict_defn(conflict,&state,&token,_) )
 
2197
  { GLS_Tok seq;
 
2198
    if( styxState_seq(state,&seq) )
 
2199
    { 
 
2200
      Pass3_checkMbrSeq(styctx,seq,qlxdfns);
 
2201
    }
 
2202
    if( styxToken_seq(token,&seq) )
 
2203
    { 
 
2204
      Pass3_checkMbrSeq(styctx,seq,qlxdfns);
 
2205
    }
 
2206
  }
 
2207
}
 
2208
 
1979
2209
static void Pass3(Styx_T styctx, styxSource src)
1980
2210
/* keywords */
1981
2211
{ PT_Itr it; GLS_Lst(styxQlxDfn) qlxdfns;
1985
2215
    PT_FORALL(it,src) 
1986
2216
    {
1987
2217
      if (PT_stateIT(it) == PT_POST) /* all reserved words */
1988
 
      { PT_Term trm = PT_termIT(it); styxMbr m;
 
2218
      { PT_Term trm = PT_termIT(it); 
 
2219
        styxMbr m; styxConflict cr;
1989
2220
        if( styx_Mbr(trm,&m) )
1990
2221
        {
1991
2222
          Pass3_checkMbr(styctx,m,qlxdfns);
1992
2223
        }
 
2224
        else
 
2225
        if( styx_Conflict(trm,&cr) )
 
2226
        {
 
2227
          Pass3_checkConflict(styctx,cr,qlxdfns);
 
2228
        }
1993
2229
      }
1994
2230
    }
1995
2231
    if( MAP_count(styctx->indtokkeys) >= 3 )
2189
2425
  }
2190
2426
}
2191
2427
 
 
2428
static void trans_Dfns_addConflict
 
2429
            (
 
2430
              Styx_T styctx, PLR_Cfg Cfg, 
 
2431
              styxConflict conflict, GLS_Lst(styxQlxDfn) qlxdfns,
 
2432
              MAP(symbol,NULL) key, MAP(symbol,NULL) exttok
 
2433
            )
 
2434
{ styxState state; styxToken token; GLS_Lst(styxRule) rules, itr;
 
2435
  if( styxConflict_defn(conflict,&state,&token,&rules) )
 
2436
  { GLS_Tok seq, id1, id2, nat;
 
2437
    string s; symbol statesym = NULL, tokensym = NULL;
 
2438
    int idx = -1; c_bool ucs4; 
 
2439
    if( styxState_seq(state,&seq) )
 
2440
    { 
 
2441
      s = normalKeyword(styctx,seq,GLS_EMPTY(qlxdfns),&ucs4);
 
2442
      statesym = stringToSymbol(s);
 
2443
      FreeMem(s);
 
2444
      if( !MAP_defined(key,statesym) )
 
2445
      {
 
2446
        PT_diag_err(seq,styctx->diag,"unknown state symbol");
 
2447
        return;
 
2448
      }
 
2449
    }
 
2450
    else
 
2451
    if( styxState_ide(state,&id1) )
 
2452
    { 
 
2453
      statesym = GLS_Tok_symbol(id1);
 
2454
      if( GLS_EMPTY(qlxdfns) && !MAP_defined(exttok,statesym) )
 
2455
      {
 
2456
        PT_diag_err(id1,styctx->diag,"unknown state symbol");
 
2457
        return;
 
2458
      }
 
2459
    }
 
2460
    else
 
2461
    if( styxState_nat(state,&nat) )
 
2462
    { 
 
2463
      idx = atoi(GLS_Tok_string(nat));
 
2464
    }
 
2465
    if( styxToken_seq(token,&seq) )
 
2466
    { 
 
2467
      s = normalKeyword(styctx,seq,GLS_EMPTY(qlxdfns),&ucs4);
 
2468
      tokensym = stringToSymbol(s);
 
2469
      FreeMem(s);
 
2470
      if( !MAP_defined(key,tokensym) )
 
2471
      {
 
2472
        PT_diag_err(seq,styctx->diag,"unknown token symbol");
 
2473
        return;
 
2474
      }
 
2475
    }
 
2476
    else
 
2477
    if( styxToken_ide(token,&id1) )
 
2478
    { 
 
2479
      tokensym = GLS_Tok_symbol(id1);
 
2480
      if( GLS_EMPTY(qlxdfns) && !MAP_defined(exttok,tokensym) )
 
2481
      {
 
2482
        PT_diag_err(id1,styctx->diag,"unknown token symbol");
 
2483
        return;
 
2484
      }
 
2485
    }
 
2486
    if( tokensym == NULL )
 
2487
    {
 
2488
      PT_diag_err(token,styctx->diag,"unknown token symbol");
 
2489
      return;
 
2490
    }
 
2491
    if( statesym == NULL && idx == -1 )
 
2492
    {
 
2493
      PT_diag_err(token,styctx->diag,"undefined conflict state");
 
2494
      return;
 
2495
    }
 
2496
    PLR_addCCtx(Cfg,idx,statesym==NULL?NULL:symbolToString(statesym),symbolToString(tokensym));
 
2497
    GLS_FORALL(itr,rules)
 
2498
    { 
 
2499
      styxRule rule = GLS_FIRST(styxRule,itr);
 
2500
      if( styxRule_red(rule,&id1,&id2) )
 
2501
      {
 
2502
        PLR_addCRule(Cfg,GLS_Tok_string(id1),GLS_Tok_string(id2));
 
2503
      }
 
2504
    }
 
2505
  }
 
2506
}
 
2507
 
2192
2508
static void trans_Dfns_addMbr_Seq
2193
2509
              (
2194
2510
                Styx_T styctx, PLR_Cfg Cfg, 
2210
2526
              )
2211
2527
2212
2528
  long r = PT_row(m), c = PT_col(m);
2213
 
  GLS_Tok id, seq;
 
2529
  GLS_Tok id, did, seq;
2214
2530
  if( styxMbr_tkm(m,&seq) )
2215
2531
  { 
2216
2532
    trans_Dfns_addMbr_Seq(styctx,Cfg,seq,qlxdfns);
2232
2548
    return C_True;
2233
2549
  }
2234
2550
  else
 
2551
  if( styxMbr_dtok(m,&id,&did) )
 
2552
  { 
 
2553
    r = PT_row(id);
 
2554
    c = PT_col(id);
 
2555
    PLR_addPD(Cfg,GLS_Tok_string(id),GLS_Tok_string(did),r,c);
 
2556
    HS_SET_ELM(GLS_Tok_symbol(id),styctx->tkset);
 
2557
    return C_True;
 
2558
  }
 
2559
  else
2235
2560
  if( styxMbr_else(m) )
2236
2561
  {
2237
2562
    PLR_addPT(Cfg,PLR_TOK_Other,r,c);
2322
2647
2323
2648
  MAPTY keyty; MAP(symbol,NULL) key, exttok; /* reserved words */
2324
2649
  PLR_Cfg Cfg; MAPIT itr; PT_Itr it; 
2325
 
  GLS_Lst(styxQlxDfn) qlxdfns; GLS_Lst(styxDfn) dfns, dfnitr; 
 
2650
  GLS_Lst(styxQlxDfn) qlxdfns; 
 
2651
  GLS_Lst(styxDfn) dfns, dfnitr; 
 
2652
  GLS_Lst(styxConflict) conflicts, critr; 
2326
2653
  styxOptCfg optcfg; styxDfn pd; styxMbr m;
2327
2654
  GLS_Tok id, nt, elan;
2328
2655
  c_bool tkother = C_False;
2329
2656
  styxSource_root(src,_,&id,&qlxdfns,&optcfg);
2330
 
  styxOptCfg_cfg(optcfg,&dfns);
 
2657
  styxOptCfg_cfg(optcfg,&dfns,&conflicts);
2331
2658
  Cfg    = PLR_createCfg(symbolToString(GLS_Tok_symbol(id)),styctx->version);
2332
2659
  keyty  = MAP_newTyp(primCopy,primFree,primEqual,primHash,primCopy,primFree);
2333
2660
  key    = MAP_newMap(keyty);
2352
2679
          FreeMem(lantok);
2353
2680
        }
2354
2681
      }
 
2682
      else
 
2683
      if( styxQlxDfn_defd(qd,&id) )
 
2684
      { int flg = 0;
 
2685
        if( MAP_defined(styctx->dyntok,GLS_Tok_symbol(id)) )
 
2686
          flg = MAP_apply(int,styctx->dyntok,GLS_Tok_symbol(id));
 
2687
        if( (flg&1) == 0 || (flg&2) == 0 )
 
2688
        {
 
2689
          PT_diag_warn
 
2690
          (
 
2691
            id,styctx->diag,"%s dynamic token %s",
 
2692
            GLS_Tok_string(id),
 
2693
            (flg&2)==0 ? "unused" : "unintroduced"
 
2694
          );
 
2695
        }
 
2696
        PLR_addTK(Cfg,GLS_Tok_string(id),PLR_TYP_TOK);
 
2697
      }
2355
2698
    }
2356
2699
    else
2357
2700
    if( styx_Dfn(t,&pd) ) /* Nonterminal */
2369
2712
  {
2370
2713
    PLR_addTK(Cfg,PLR_TOK_Other,PLR_TYP_TOK);
2371
2714
  }
 
2715
 
 
2716
  /*
2372
2717
  MAP_freeMap( key );
2373
2718
  MAP_freeMap( exttok );
2374
2719
  MAP_freeTyp( keyty );
 
2720
  */
2375
2721
 
2376
2722
  PLR_endSD(Cfg); /* Sorting Token & Nonterminals */
2377
2723
 
2378
2724
  MAP_forItr(id,itr,styctx->glo)
2379
 
    if( styx_QlxDfn(MAP_apply(PT_Term,styctx->glo,id),_) && 
 
2725
  { styxQlxDfn qd;
 
2726
    if( styx_QlxDfn(MAP_apply(PT_Term,styctx->glo,id),&qd) && 
 
2727
        styxQlxDfn_defn(qd,_,_,_,_,_,_) &&
2380
2728
        qlx_usage(styctx,id) == UsageCom ) /* Special Token */
2381
2729
      PLR_addST(Cfg,GLS_Tok_string(id));
 
2730
  }
2382
2731
  GLS_FORALL(dfnitr,dfns)
2383
2732
  { styxCat c; GLS_Lst(styxPrd) prds, prditr; 
2384
2733
    pd = GLS_FIRST(styxDfn,dfnitr);
2425
2774
      trans_Dfns_addEBNFPrd(styctx,Cfg,ep,qlxdfns);
2426
2775
    }
2427
2776
  }
 
2777
 
 
2778
  /* Conflict rules */
 
2779
 
 
2780
  GLS_FORALL(critr,conflicts)
 
2781
  { styxConflict conflict = GLS_FIRST(styxConflict,critr);
 
2782
    trans_Dfns_addConflict
 
2783
    (
 
2784
      styctx, Cfg, conflict, qlxdfns, key, exttok
 
2785
    );
 
2786
  }
 
2787
 
 
2788
  MAP_freeMap( key );
 
2789
  MAP_freeMap( exttok );
 
2790
  MAP_freeTyp( keyty );
 
2791
 
2428
2792
  return Cfg;
2429
2793
}
2430
2794
 
2448
2812
  { 
2449
2813
    MAP_freeMap(styctx->grptok); styctx->grptok = (MAP(_,_))NULL;
2450
2814
  }
 
2815
  if( styctx->dyntok != (MAP(_,_))NULL ) 
 
2816
  { 
 
2817
    MAP_freeMap(styctx->dyntok); styctx->dyntok = (MAP(_,_))NULL;
 
2818
  }
2451
2819
  if( styctx->langrp != (MAP(_,_))NULL ) 
2452
2820
  { 
2453
2821
    MAP_freeMap(styctx->langrp); styctx->langrp = (MAP(_,_))NULL;
2534
2902
    styctx->indtokkeys = MAP_newPrimMap();
2535
2903
    styctx->grp        = MAP_newPrimMap();
2536
2904
    styctx->grptok     = MAP_newMap(styctx->gloty);
 
2905
    styctx->dyntok     = MAP_newPrimMap();
2537
2906
    styctx->langrp     = MAP_newPrimMap();
2538
2907
    styctx->tokgrp     = MAP_newMap(styctx->gloty);
2539
2908
    styctx->ebnfmbr    = MAP_newMap(styctx->gloty);
2591
2960
      {
2592
2961
        /* CFG translator */
2593
2962
  
2594
 
        if( styxOptCfg_cfg(optcfg,&dfns) && ! GLS_EMPTY(dfns) )
 
2963
        if( styxOptCfg_cfg(optcfg,&dfns,_) && ! GLS_EMPTY(dfns) )
2595
2964
        {
2596
 
          styctx->PTab = PLR_createTab
2597
 
                         (
2598
 
                           (Cfg=trans_Dfns(styctx,s)),
2599
 
                           styctx->verbose,styctx->diagnose
2600
 
                         );
 
2965
          Cfg = trans_Dfns(styctx,s);
 
2966
          if( (styctx->errorCnt=PT_diag_errorCnt(styctx->diag)) == 0 )
 
2967
          {
 
2968
            styctx->PTab = PLR_createTab(Cfg,styctx->verbose,styctx->diagnose);
 
2969
          }
2601
2970
          PLR_delCfg(Cfg);
2602
2971
  
2603
2972
          if( styctx->PTab != (PLR_Tab)NULL )