⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cr.atg

📁 COCO類似C的編譯器
💻 ATG
📖 第 1 页 / 共 2 页
字号:
$C  /* Generate Compiler Module */
COMPILER CR
/**************************************************************************
   COCO/R for C grammar used to generate COCO/R itself
   1.16 Last modified Thu  11-16-00
**************************************************************************/

#include "crt.h"
#include "crf.h"
#include "cra.h"
#include "crp.h"
#include "crs.h"
#include <string.h>
#include <stdlib.h>

static void FixString(char *name)
{
  int i, j, len, spaces, start;
  len = strlen(name);
  if (len == 2) { SemError(129); return; }
  if (ignore_case) upcase(name);
  spaces = FALSE; start = name[0];
  for (i = 1; i <= len-2; i++) {
    if (name[i] > 0 && name[i] <= ' ') spaces = TRUE;
    if (name[i] == '\\') {
      if (name[i+1] == '\\' || name[i+1] == '\'' || name[i+1] == '\"') {
        for (j = i; j < len; j++) name[j] = name[j+1]; len--;
      }
    }
  }
  if (spaces) SemError(124);
}

static void MatchLiteral (int sp)
/* store string either as token or as literal */
{
  PTermNode sn, sn1;
  int matched_sp;

  sn = GetTermP(sp);
  matched_sp = MatchDFA((unsigned char *) sn->name, sp);
  if (matched_sp != 0) {
    sn1 = GetTermP(matched_sp);
    sn1->type = T_CLASSLITTOKEN;
    sn->type  = T_LITTOKEN;
  } else sn->type= T_CLASSTOKEN;
}

static void SetCtx (int gp)
/* set transition code to contextTrans */
{
  PGraphNode gn;
  while (gp > 0) {
    gn = GetGraphP(gp);
    if (gn->type == T_CHAR || gn->type == T_CLASS)
      gn->CONTEXT = T_CONTEXT;
    else
      if (gn->type == T_OPT || gn->type == T_REP)
      SetCtx(gn->INNER);
      else
      if (gn->type == T_ALT) {
      SetCtx(gn->INNER); SetCtx(gn->ALT);
      }
    gp = gn->next;
  }
}

static void StringClass(char *s, Set *items)
{
  s[strlen(s)-1]=0;  s++;  /* Ignore First and Last character */
  while (*s) Set_AddItem(items, *s++);
}

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

CHARACTERS
  letter   = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_" .
  digit    = "0123456789" .
  cntl     = CHR(0)..CHR(31).
  tab      = CHR(9) .
  eol      = CHR(13).
  lf       = CHR(10) .
  back     = CHR(92) .
  noQuote1 = ANY - '"' - cntl - back .
  noQuote2 = ANY - "'" - cntl - back .
  graphic  = ANY - cntl .

IGNORE tab + eol + lf

TOKENS
  ident     = letter {letter | digit} .
  string    =   '"' {noQuote1 | back graphic } '"'
              | "'" {noQuote2 | back graphic } "'" .
  badstring =   '"' {noQuote1 | back graphic } ( eol | lf )
              | "'" {noQuote2 | back graphic } ( eol | lf ) .
  number    = digit {digit} .

NAMES
  Range   = "..".

PRAGMAS
  Options = "$" {letter}.
                                (. char s[100];
                                   LookAheadString(s, sizeof(s)-1);
                                   SetOptions(s); .)

COMMENTS
  FROM "/*" TO "*/" NESTED

PRODUCTIONS

CR =                            (. Name name1;
                                   int  attr, sem, exp, is_new, sp, type;
                                   int startedDFA = FALSE;
                                   PNTermNode sn; .)

   "COMPILER"
   Ident<compiler_name>         (. global_defs.pos  = S_NextPos;
                                   global_defs.line = S_NextLine; .)
   { ANY }                      (. global_defs.len  =
                                     (int) (S_NextPos-global_defs.pos); .)
   { Declaration<&startedDFA> }
   SYNC                         (. if (Successful()) { /* No Errors so far */
                                     if (!MakeDeterministic()) SemError(127);
                                   } .)
   "PRODUCTIONS"
   {                            (. attr = NIL; sem = NIL; .)
      Ident<name1>              (. if ((sp = FindSym(name1, &type)) != UNDEF) {
                                     is_new = FALSE;
                                     if (type != T_NT) { SemError(108); return; }
                                     else {
                                       sn = GetNTermP(sp);
                                       if (sn->graph) SemError(107);
                                       sn->line_dec = S_Line;
                                     }
                                   } else {
                                     sp = NewSym(name1, T_NT);
                                     sn = GetNTermP(sp); is_new = TRUE;
                                     sn->line_dec = S_Line;
                                   } .)
      [ Attribs<&attr> ]        (. if (!is_new) {
                                     if (sn->has_attr && !attr) SemError(105);
                                     if (!sn->has_attr && attr) SemError(105);
                                   }
                                   if (attr) {
                                     sn->attr = attr; sn->has_attr = TRUE;
                                   } .)
      WEAK "="
      [ SemText<&sem> ]
      Expression<&exp>          (. if (sem) {
                                     (void) LinkGraph(sem, exp); exp = sem;
                                   } .)
      WEAK "."                  (. sn = GetNTermP(sp); /* reload */
                                   sn->graph = exp; .)
      SYNC
   }
   "END" Ident<name1>           (. if (strcmp(name1, compiler_name)) SemError(117);
                                   if((sp = FindSym(compiler_name, &type)) != UNDEF) {
                                     if (type!=T_NT) SemError(108);
                                     else {
                                       sn = GetNTermP(sp);
                                       if (sn->has_attr) SemError(112);
                                       sn->reachable=TRUE;
                                     }
                                   } else SemError(111);
                                   no_sym = NewSym("not", T_T); .)
   "."                          (. if (dirty_DFA && !MakeDeterministic()) SemError(127); .)
   .

Declaration<int *startedDFA> =
                                (. Set ignore;
                                   int n1, n2, nested = FALSE; .)
  (
      "CHARACTERS" { SetDecl }
    | "TOKENS"   { TokenDecl<T_T> }
    | "NAMES"    { NameDecl }
    | "PRAGMAS"  { TokenDecl<T_P> }
    | "COMMENTS"
      "FROM" TokenExpr<&n1>
      "TO"   TokenExpr<&n2>
      [ "NESTED"                (. nested = TRUE; .)
      ]                         (. if (n1 * n2) NewComment(n1, n2, nested); .)
    | "IGNORE"
      ( "CASE"                  (. if (*startedDFA) SemError(130);
                                   ignore_case = TRUE; .)
       |                        (. Set_Init(&ignore); .)
        CompSet<&ignore>        (. AddIgnore(&ignore);
                                   if (Set_IsItem(&ignore,0)) SemError(119);
                                   Set_Done(&ignore); .)
      )
   )                            (. *startedDFA = TRUE; .)
   .

SetDecl =
                                (. Name name;
                                   Set  items;
                                   Set_Init(&items); .)

   Ident<name>                  (. if (FindClass(name) != UNDEF) SemError(107); .)
   "=" CompSet<&items> "."      (. (void) NewClass(name, &items);
                                   Set_Done(&items); .)
   .

CompSet<PSet items> =
                                (. Set set1, set2;
                                   Set_Init(&set1); Set_Init(&set2); .)
   SimSet<&set1>
   { (  "+" SimSet<&set2>       (. Set_Union(&set1, &set2); .)
      | "-" SimSet<&set2>       (. Set_Diference(&set1, &set2); .)
     )                          (. Set_Clean(&set2); .)
   }                            (. Set_Union(items, &set1);
                                   Set_Done(&set1); Set_Done(&set2); .)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -