Bison/Yacc中的For循环语义 [英] For loop semantics in Bison/Yacc

查看:95
本文介绍了Bison/Yacc中的For循环语义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用flex和bison编写自己的脚本语言.我有一个基本的解析器,我想添加一个与C语言for语句非常相似的for语句.我不清楚如何编码与for语句关联的动作

I'm trying to write my own scripting language using flex and bison. I have a basic parser and I would like to add a for statement very similar to the C language for statement. It is not clear to me how to code the action associated to the for statement

假设"for"语句的生产如下:

Suppose I have the following production for the 'for' statement:

forStatement: FOR '(' expr ';' expr ';' expr ')' statements END; {}

我不清楚在与此制作相关的动作中该怎么做.

It is not clear to me what to do in the action associated to this production.

直觉上,我理解我应该在与上一条语句相关的操作中做一些事情,例如:

Intuitively I understand that I should do something, in the action associated to the previous statement, such as :

evaluate($3);
while(evaluate($5)) { execute($9); evaluate($7); }
evaluate($7);

评估和执行是两个C函数.

where evaluate and execute are two C functions.

所以我有两个问题(假设为与语法产生相关的动作编写C代码):

So I have two questions (suppose to write C code for the action associated to the grammar production):

  1. 评估"的任务是什么.我的意思是,考虑到表达式的值在循环的每一步都可能发生变化,我该如何在每个循环中评估表达式?
  2. 执行"的任务是什么.我的意思是,考虑到每个语句在循环的每一步都有不同的结果,我该如何评估for循环内的语句.

三个表达式'expr'的值在运行时都会更改,并且for主体中的语句也是如此.

The values of the three expressions 'expr' changes at runtime and the same is true for the statements inside the for body.

推荐答案

看着您的两个问题,您似乎错过了需要执行引擎(一种软件CPU)的问题.该引擎需要记住变量的状态,返回循环的地址",...(取决于选择的实现方法).

Looking at your two questions, you seem to miss that an execution engine (a kind of software CPU) is needed. This engine needs to remember the state of variables, return 'addresses' of loops, ... (depending on implementation method chosen).

因此,解析只是第一步.考虑为每个语句和表达式创建一个数据结构,并由该引擎执行它们.

So parsing is just the first step. Think about creating a data structure for each statement and expression, and have these executed by this engine.

看看基于堆栈的系统.

这篇关于Bison/Yacc中的For循环语义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆