COCO/R的一个例子(生成中间语言)

摘自COCO/R手册,代码具有启发性

Sources of the Sample Compiler
B.1 Taste.ATG
COMPILER Taste
const int // types
undef = 0, integer = 1, boolean = 2;
const int // object kinds
var = 0, proc = 1;
public SymbolTable tab;
public CodeGenerator gen;
CHARACTERS
letter = 'A'..'Z' + 'a'..'z'.
digit = '0'..'9'.
TOKENS
ident = letter {letter | digit}.
number = digit {digit}.
COMMENTS FROM "/*" TO "*/" NESTED
COMMENTS FROM "//" TO '\n'
IGNORE '\r' + '\n' + '\t'
PRODUCTIONS
AddOp<out Op op>
= (. op = Op.ADD; .)
( '+'
| '-' (. op = Op.SUB; .)
).
/*------------------------------------------------------------------------*/
Expr<out int type> (. int type1; Op op; .)
= SimExpr<out type>
[ RelOp<out op>
SimExpr<out type1> (. if (type != type1) SemErr("incompatible types");
gen.Emit(op); type = boolean; .)
].
/*------------------------------------------------------------------------*/
Factor<out int type> (. int n; Obj obj; string name; .)
= (. type = undef; .)
( Ident<out name> (. obj = tab.Find(name); type = obj.type;
if (obj.kind == var) {
if (obj.level == 0) gen.Emit(Op.LOADG, obj.adr);
else gen.Emit(Op.LOAD, obj.adr);
} else SemErr("variable expected"); .)
| number (. n = Convert.ToInt32(t.val);
gen.Emit(Op.CONST, n); type = integer; .)
| '-'
Factor<out type> (. if (type != integer) {
SemErr("integer type expected"); type = integer;
}
gen.Emit(Op.NEG); .)
| "true" (. gen.Emit(Op.CONST, 1); type = boolean; .)
| "false" (. gen.Emit(Op.CONST, 0); type = boolean; .)
).
/*------------------------------------------------------------------------*/
Ident<out string name>
= ident (. name = t.val; .).
/*------------------------------------------------------------------------*/
35
MulOp<out Op op>
= (. op = Op.MUL; .)
( '*'
| '/' (. op = Op.DIV; .)
).
/*------------------------------------------------------------------------*/
ProcDecl (. string name; Obj obj; int adr; .)
= "void"
Ident<out name> (. obj = tab.NewObj(name, proc, undef); obj.adr = gen.pc;
if (name == "Main") gen.progStart = gen.pc;
tab.OpenScope(); .)
'(' ')'
'{' (. gen.Emit(Op.ENTER, 0); adr = gen.pc - 2; .)
{ VarDecl | Stat }
'}' (. gen.Emit(Op.LEAVE); gen.Emit(Op.RET);
gen.Patch(adr, tab.topScope.nextAdr);
tab.CloseScope(); .).
/*------------------------------------------------------------------------*/
RelOp<out Op op>
= (. op = Op.EQU; .)
( "=="
| '<' (. op = Op.LSS; .)
| '>' (. op = Op.GTR; .)
).
/*------------------------------------------------------------------------*/
SimExpr<out int type> (. int type1; Op op; .)
= Term<out type>
{ AddOp<out op>
Term<out type1> (. if (type != integer || type1 != integer)
SemErr("integer type expected");
gen.Emit(op); .)
}.
/*------------------------------------------------------------------------*/
Stat (. int type; string name; Obj obj;
int adr, adr2, loopstart; .)
= Ident<out name> (. obj = tab.Find(name); .)
( '=' (. if (obj.kind != var) SemErr("cannot assign to procedure"); .)
Expr<out type> ';' (. if (type != obj.type) SemErr("incompatible types");
if (obj.level == 0) gen.Emit(Op.STOG, obj.adr);
else gen.Emit(Op.STO, obj.adr); .)
| '(' ')' ';' (. if (obj.kind != proc) SemErr("object is not a procedure");
gen.Emit(Op.CALL, obj.adr); .)
)
| "if"
'(' Expr<out type> ')' (. if (type != boolean) SemErr("boolean type expected");
gen.Emit(Op.FJMP, 0); adr = gen.pc - 2; .)
Stat
[ "else" (. gen.Emit(Op.JMP, 0); adr2 = gen.pc - 2;
gen.Patch(adr, gen.pc);
adr = adr2; .)
Stat
] (. gen.Patch(adr, gen.pc); .)
| "while" (. loopstart = gen.pc; .)
'(' Expr<out type> ')' (. if (type != boolean) SemErr("boolean type expected");
gen.Emit(Op.FJMP, 0); adr = gen.pc - 2; .)
Stat (. gen.Emit(Op.JMP, loopstart); gen.Patch(adr, gen.pc); .)
| "read"
Ident<out name> ';' (. obj = tab.Find(name);
if (obj.type != integer) SemErr("integer type expected");
gen.Emit(Op.READ);
if (obj.level == 0) gen.Emit(Op.STOG, obj.adr);
else gen.Emit(Op.STO, obj.adr); .)
36
| "write"
Expr<out type> ';' (. if (type != integer) SemErr("integer type expected");
gen.Emit(Op.WRITE); .)
| '{' { Stat | VarDecl } '}' .
/*------------------------------------------------------------------------*/
Taste (. string name; .)
= "program" (. gen.Init(); tab.Init(); .)
Ident<out name> (. tab.OpenScope(); .)
'{'
{ VarDecl | ProcDecl }
'}' (. tab.CloseScope();
if (gen.progStart == -1) SemErr("main function never defined");
.).
/*------------------------------------------------------------------------*/
Term<out int type> (. int type1; Op op; .)
= Factor<out type>
{ MulOp<out op>
Factor<out type1> (. if (type != integer || type1 != integer)
SemErr("integer type expected");
gen.Emit(op); .)
}.
/*------------------------------------------------------------------------*/
Type<out int type>
= (. type = undef; .)
( "int" (. type = integer; .)
| "bool" (. type = boolean; .)
).
/*------------------------------------------------------------------------*/
VarDecl (. string name; int type; .)
= Type<out type>
Ident<out name> (. tab.NewObj(name, var, type); .)
{ ',' Ident<out name> (. tab.NewObj(name, var, type); .)
} ';'.
END Taste.
37
B.2 SymTab.cs (symbol table)
using System;
namespace Taste {
public class Obj { // object decribing a declared name
public string name; // name of the object
public int type; // type of the object (undef for procs)
public Obj next; // to next object in same scope
public int kind; // var, proc, scope
public int adr; // address in memory or start of proc
public int level; // nesting level; 0=global, 1=local
public Obj locals; // scopes: to locally declared objects
public int nextAdr; // scopes: next free address in this scope
}
public class SymbolTable {
const int // types
undef = 0, integer = 1, boolean = 2;
const int // object kinds
var = 0, proc = 1, scope = 2;
public int curLevel; // nesting level of current scope
public Obj undefObj; // object node for erroneous symbols
public Obj topScope; // topmost procedure scope
Parser parser;
// open a new scope and make it the current scope (topScope)
public void OpenScope () {
Obj scop = new Obj();
scop.name = ""; scop.kind = scope;
scop.locals = null; scop.nextAdr = 0;
scop.next = topScope; topScope = scop;
curLevel++;
}
// close the current scope
public void CloseScope () {
topScope = topScope.next; curLevel--;
}
// create a new object node in the current scope
public Obj NewObj (string name, int kind, int type) {
Obj p, last, obj = new Obj();
obj.name = name; obj.kind = kind; obj.type = type;
obj.level = curLevel;
p = topScope.locals; last = null;
while (p != null) {
if (p.name == name) parser.SemErr("name declared twice");
last = p; p = p.next;
}
if (last == null) topScope.locals = obj; else last.next = obj;
if (kind == var) obj.adr = topScope.nextAdr++;
return obj;
}
38
// search the name in all open scopes and return its object node
public Obj Find (string name) {
Obj obj, scope;
scope = topScope;
while (scope != null) { // for all scopes
obj = scope.locals;
while (obj != null) { // for all objects in this scope
if (obj.name == name) return obj;
obj = obj.next;
}
scope = scope.next;
}
parser.SemErr(name + " is undeclared");
return undefObj;
}
public SymbolTable (Parser parser) {
this.parser = parser;
topScope = null;
curLevel = -1;
undefObj = new Obj();
undefObj.name = "undef"; undefObj.type = undef; undefObj.kind = var;
undefObj.adr = 0; undefObj.level = 0; undefObj.next = null;
}
} // end SymbolTable
} // end namespace
39
B.3 CodeGen.cs (code generator)
using System;
using System.IO;
namespace Taste {
public enum Op { // opcodes
ADD, SUB, MUL, DIV, EQU, LSS, GTR, NEG,
LOAD, LOADG, STO, STOG, CONST,
CALL, RET, ENTER, LEAVE, JMP, FJMP, READ, WRITE
}
public class CodeGenerator {
string[] opcode =
{"ADD ", "SUB ", "MUL ", "DIV ", "EQU ", "LSS ", "GTR ", "NEG ",
"LOAD ", "LOADG", "STO ", "STOG ", "CONST", "CALL ", "RET ", "ENTER",
"LEAVE", "JMP ", "FJMP ", "READ ", "WRITE"};
public int progStart; // address of first instruction of main program
public int pc; // program counter
byte[] code = new byte[3000];
// data for Interpret
int[] globals = new int[100];
int[] stack = new int[100];
int top; // top of stack
int bp; // base pointer
//----- code generation methods -----
public void Put(int x) { code[pc++] = (byte)x; }
public void Emit (Op op) { Put((int)op); }
public void Emit (Op op, int val) { Emit(op); Put(val>>8); Put(val); }
public void Patch (int adr, int val) {
code[adr] = (byte)(val>>8); code[adr+1] = (byte)val;
}
public void Decode() {
int maxPc = pc; pc = 1;
while (pc < maxPc) {
Op code = (Op)Next();
Console.Write("{0,3}: {1} ", pc-1, opcode[(int)code]);
switch(code) {
case Op.LOAD: case Op.LOADG: case Op.CONST: case Op.STO: case Op.STOG:
case Op.CALL: case Op.ENTER: case Op.JMP: case Op.FJMP:
Console.WriteLine(Next2()); break;
case Op.ADD: case Op.SUB: case Op.MUL: case Op.DIV: case Op.NEG:
case Op.EQU: case Op.LSS: case Op.GTR: case Op.RET: case Op.LEAVE:
case Op.READ: case Op.WRITE:
Console.WriteLine(); break;
}
}
}
//----- interpreter methods -----
int Next () {
return code[pc++];
}
int Next2 () {
int x, y;
x = (sbyte)code[pc++]; y = code[pc++];
return (x << 8) + y;
}
40
int Int (bool b) {
if (b) return 1; else return 0;
}
void Push (int val) {
stack[top++] = val;
}
int Pop() {
return stack[--top];
}
int ReadInt(FileStream s) {
int ch, sign, n = 0;
do {ch = s.ReadByte();} while (!(ch >= '0' && ch <= '9' || ch == '-'));
if (ch == '-') {sign = -1; ch = s.ReadByte();} else sign = 1;
while (ch >= '0' && ch <= '9') {
n = 10 * n + (ch - '0');
ch = s.ReadByte();
}
return n * sign;
}
public void Interpret (string data) {
int val;
try {
FileStream s = new FileStream(data, FileMode.Open);
Console.WriteLine();
pc = progStart; stack[0] = 0; top = 1; bp = 0;
for (;;) {
switch ((Op)Next()) {
case Op.CONST: Push(Next2()); break;
case Op.LOAD: Push(stack[bp+Next2()]); break;
case Op.LOADG: Push(globals[Next2()]); break;
case Op.STO: stack[bp+Next2()] = Pop(); break;
case Op.STOG: globals[Next2()] = Pop(); break;
case Op.ADD: Push(Pop()+Pop()); break;
case Op.SUB: Push(-Pop()+Pop()); break;
case Op.DIV: val = Pop(); Push(Pop()/val); break;
case Op.MUL: Push(Pop()*Pop()); break;
case Op.NEG: Push(-Pop()); break;
case Op.EQU: Push(Int(Pop()==Pop())); break;
case Op.LSS: Push(Int(Pop()>Pop())); break;
case Op.GTR: Push(Int(Pop()<Pop())); break;
case Op.JMP: pc = Next2(); break;
case Op.FJMP: val = Next2(); if (Pop()==0) pc = val; break;
case Op.READ: val = ReadInt(s); Push(val); break;
case Op.WRITE: Console.WriteLine(Pop()); break;
case Op.CALL: Push(pc+2); pc = Next2(); break;
case Op.RET: pc = Pop(); if (pc == 0) return; break;
case Op.ENTER: Push(bp); bp = top; top = top + Next2(); break;
case Op.LEAVE: top = bp; bp = Pop(); break;
default: throw new Exception("illegal opcode");
}
}
} catch (IOException) {
Console.WriteLine("--- Error accessing file {0}", data);
System.Environment.Exit(0);
}
}
public CodeGenerator () { pc = 1; progStart = -1; }
} // end CodeGen
} // end namespace
41
B.4 Taste.cs (main program)
using System;
namespace Taste {
class Taste {
public static void Main (string[] arg) {
if (arg.Length > 0) {
Scanner scanner = new Scanner(arg[0]);
Parser parser = new Parser(scanner);
parser.tab = new SymbolTable(parser);
parser.gen = new CodeGenerator();
parser.Parse();
if (parser.errors.count == 0) {
parser.gen.Decode();
parser.gen.Interpret("Taste.IN");
}
} else {
Console.WriteLine("-- No source file specified");
}
}
}
} // end namespace

【COCO数据集】COCO API入门 超详细注解 A Glance through COCO Presented By R.G. 关于COCO COCO的官网:https://cocodataset.org/ COCO API : https://github.com/cocodataset/cocoapi 以下是 COCO API 的 README COCO is a large image dataset designed for object detection, segmentation, person keypoints detection, s 阅读详情

相关推荐

【干货】用mmdetection,在COCO数据集上跑通faster R-CNN(测试、训练)

注:刚接触R-CNN、Fast R-CNN、Faster R-CNN,就尝试用mmdetection,在coco数据集上跑Faster R-CNN,训练测试完就来记录一下这个过程的一些坑 参考 mmdetection github网址 跑通mmdetection 安装Anaconda3遇到的几个问题 关于跑通mmdetection,建议主要看第一个网址,第二个网址参考为主,因为我按原博的步骤...

Activewaste 2万+

Coco/R快速编译器生成

Coco/R 更Cool些,Coco/R 使用增强的 EBNF 文法建立递归下降分析的分析器(Recursive Descent Parser)和扫描器(Scanner)。Coco/R 除了提供调用分析器的主程序模块之外,还提供语义(semantic )模块在文法之中调用(如,符号表处理和代码生成器),可以直接在grammer 内写语意动作【用你自己喜欢的语言。俺是中意Delphi】。Coco/R

深未来技术 1002

如何比较两个相关系数的显著性?

当我们计算得到两个相关系数时,我们可能会想要比较它们的显著性,以确定它们之间的差异是否是由随机因素引起的。使用这些方法,我们可以更好地理解和解释两个相关系数之间的差异,并评估其在统计上的显著性。现在,我们可以使用Cocor包中的相关系数比较函数来比较r1和r2的显著性。上述代码将使用Steiger的Z变换方法对r1和r2进行比较,并返回显著性检验的结果。假设我们有两组数据,分别为X和Y,我们计算得到的相关系数分别为r1和r2。下面我将介绍如何使用Cocor包来比较两个相关系数的显著性,并提供相应的源代码。

web_bug407的博客 1133

制作自己的COCO数据集,训练mask R-CNN

先占个坑,国庆假期的时候来写。 10月16号,终于打开了CSDN,想起了还有一篇没写的博客 本文适合一些小白看,比较简单,稍微改一下别人的示例代码就行。 之前做了点肿瘤检测的工作,就是在图上把肿瘤的位置用边界框标出来,用自己的数据做成了VOC格式,然后训练了faster R-CNN,结果还行。下面要做的工作是在之前的基础上,还要把肿瘤勾画出来,所以就要用到mask R-CNN了,这时候数据就...

u010684651的博客 1万+

RT-DETR 系列性能实测:R18/R50 在 COCO 数据集上的表现

RT-DETR系列在COCO数据集上展现了卓越的实时检测性能。RT-DETR-R18以28ms推理速度达到46.2% mAP,RT-DETR-R50在35ms内实现53.1% mAP,显著优于传统DETR模型(120ms/44.9% mAP)。测试环境配置包括高端GPU(RTX 4090)、PyTorch 2.0框架和标准COCO评测工具。性能测试框架采用COCOEvaluator类,支持完整指标评估流程,包含IOU阈值设置(0.5-0.95)和最大检测数配置。该系列模型在速度-精度平衡上取得突破,为实时高

走向CTO的路上... 1532

Coco/R解析器生成器的深入解析

本文深入探讨了Coco/R解析器生成器的工作原理及其在编译器开发中的应用。通过分析具体的语法规则和动作,展示了如何使用Coco/R来构建一个功能完整的解析器。文章详细解释了语法同步点、错误恢复机制、语义错误处理以及如何与支持模块接口的交互。最终,通过一个案例研究的解析器示例,说明了如何整合扫描器和解析器到一个完整的驱动程序中。...

weixin_32306683的博客 555

coco-r-cpp:我的C ++ CocoR编译器生成器镜像

可可/ R 这是C ++的编译器生成器。 要生成二进制文件,请运行 make 可执行文件将在此目录中。 根据需要使用它。 可从Coco / R的原始站点获得文档。

深入理解编译器生成工具Coco/R的应用案例

本文深入探讨了编译器生成工具Coco/R的应用案例,通过分析其如何处理复杂的C语言声明、生成单地址代码以及从AST生成代码的过程,揭示了编译器设计中的关键技巧和策略。读者将了解到如何利用Coco/R来简化编译器的编写工作,并对编译器的内部工作原理有更深刻的理解。...

weixin_32306683的博客 484

coco/R lex和yacc类似功能的c#版本

coco /R lex和yacc类似功能的c#版本

构建编译器:Coco/R语法分析器的深入探索

本文介绍了Coco/R工具在语法分析和词法分析器生成中的应用。通过案例研究,展示了如何使用Cocol语言定义文法,并且详细探讨了产生式、标记、字符集、注释和可忽略字符等概念。文章强调了理解Cocol规范语言的重要性,并通过实例说明了如何通过Coco/R生成解析器和扫描器代码。...

weixin_32306683的博客 443

编译器原理与Coco/R工具应用解析

本文是关于计算机编译器的详细解读,涵盖了编译器的分类、结构、构造以及自举过程。同时,重点介绍了Coco/R编译器生成器的使用和案例研究,以C++语言为例,深入探讨了编译器前端与后端的关键技术和实现方式。...

weixin_32306683的博客 480

COCO-Style 数据集生成器 GUI 使用教程

COCO-Style 数据集生成器 GUI 使用教程 项目介绍 COCO-Style 数据集生成器 GUI 是一个基于图形用户界面的工具,用于生成 COCO 风格的多边形掩码标注文件。该工具旨在通过提供一个简单易用的界面,帮助用户快速高效地创建标注掩码和边界框。此外,用户还可以选择使用预训练的 Mask R-CNN 模型来生成初始分割。 项目快速启动 安装 首先,确保你已经安装了 Python 环...

gitblog_00310的博客 1000

Coco/S Parser Generator:基于Coco / R的解析器生成器-开源

Coco / S是一个编译器生成器,它采用纯EBNF语法文件并具有SAX样式回调API。 它是用Java编写的,并针对输入语法所描述的语言生成了Java扫描器(Lexer)和解析器。 Coco / S是Coco / R for Java的2010/11版本的分支。 虽然Coco / R带有属性语法文件,但Coco / S具有回调API。 这具有语法(EBNF)与编译器/解释器之间清晰分离的优势。

制作自己的COCO格式数据集,附代码!

最近做了一个细胞检测的练习项目。之前的思路是参考其他大神的代码,后来发现其他人的代码有很多自定义的内容,包括读取的数据格式等等,小白表示看不懂所以改变思路,用最简单的方法——选择mmdetection2.0中自带的Faster RNN网络进行训练。但是网络对数据格式有要求,有VOC、COCO等几种格式,我选择了COCO格式。 一、COCO2017数据集格式 COCO_ROOT #根目录 ├── annotations # 存放json格式的标注 │ ...

weixin_44052271的博客 2万+

coco/r 的官方资料

想研究编译原理和开发编译器的朋友可以使用,支持生成java、C#、C++ 等分析器代码,包括词法分析和语法分析

Coco/R Plugin for NetBeans-开源

这个NetBeans插件在NetBeans IDE中增加了对Coco / R的支持。 Coco / R是一个编译器生成器,它采用属性语法,并为语法描述的语言提供扫描程序和解析器。

Mask_RCNN训练自己的数据——制作COCO格式的数据集

matterport版mask_rcnn系列: 1.Mask_RCNN训练自己的数据 2.目标检测:使用Open_cv在图像上批量画bounding box 3.Mask_RCNN:使用COCO权重进行特定类别预测(只标记出你需要的类别) 4.Mask_RCNN:ValueError: Dimension 1 in both shapes ...............

地理信息系统、摄影测量与遥感、人工智能 4万+

高效低功耗设计:PMOS电子开关在墨水屏与ADC采样电路中的创新应用

本文深入探讨了PMOS电子开关在低功耗设计中的创新应用,特别是在墨水屏和ADC采样电路中的关键作用。通过详细分析PMOS的导通机制和近乎零静态功耗特性,展示了其在待机低功耗场景下的显著优势,并提供了实用的电路设计技巧和元器件选型建议,帮助工程师实现极致的功耗优化。

weixin_29193359的博客 174
上一篇: veket随身系统》(veket linux)2.3.1(增加办公专用版和开发专用版)
下一篇: WIN32汇编-开发记事本语音阅读工具
deepfuture
博客等级 码龄17年 211粉丝 1287原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址