复习recursion

1. 如果这样数列赋值在recursion之前,则:

private void lop(int x, int size, double[] jerry){
		if(x==size-1){
			jerry[x] = -1;
			return;
		}
		if(jerry[x+1]==0){
			jerry[x+1] = jerry[x]- 1;  //if so, then jerry={1,1,3.14}
			lop(x+1, size, jerry);
		}
		//jerry[x] = jerry[x+1]+ 1;
	}

2. 如果赋值在recursion最后,则:(本意)

private void lop(int x, int size, double[] jerry){
		if(x==size-1){
			jerry[x] = -1;
			return;
		}
		if(jerry[x+1]==0){
			//jerry[x+1] = jerry[x]- 1;  
			lop(x+1, size, jerry);
		}
		jerry[x] = jerry[x+1]+ 1;  //if so, then jerry={5.14,4.14,3.14}
	}

3. 感觉recursion很容易漏掉某些值。所以要多次对instance调用。

/* May 10,2014
 * to understand seamcarver by msi-pl
 * learn how recur in for loop
 */

public class recur{
	public recur(){
		
	}
	private void lop(int x, int y, int size, double[][] jing){
		double feed = 3.14;
		if(y==size-1){
			if(x==0)
				jing[x][y] = feed;
			if(x==1)
				jing[x][y] = feed*10;
			if(x==2)
				jing[x][y] = feed*100;	
			return;
		}
		for(int m = 0; m <size; m++){
			int px = (x+m)%3;
			//if(px >= size)
				//px = 0;
				//continue;
			if(jing[px][y+1]==0){
				lop(px, y+1, size, jing);
			}
		}
		jing[x][y] = jing[x][y+1]+ 1;
	}
	
	public void sh(int dx){
		recur jerry = new recur();
		double[][] tony = new double[dx][dx];
		jerry.lop(0, 0, dx, tony);
		jerry.lop(1, 0, dx, tony);  // need to do so for jerry[1][0];
		jerry.lop(2, 0, dx, tony);  //need to do so for jerry[2][0];
		
		for(int i = 0; i<dx; i++){
			for(int j=0; j<dx; j++){
				StdOut.println(tony[i][j]);
			}
		}
	}
	public static void main(String[] args){
		recur shishi = new recur();
		shishi.sh(3);
	}
}


### 编译原理期末考试复习资料中的大题 #### 词法分析器设计 构建一个简单的词法分析器来识别特定类型的单词符号。假设有一个小型编程语言,其关键字包括`if`, `else`, `while`, 和`return`。该词法分析器应能读取源文件并将这些关键词以及标识符、整数常量和其他基本符号转换为相应的标记。 对于此任务,可以采用有限状态自动机(FSA)的方式来进行模式匹配[^1]: ```python import re def lexer(source_code): tokens_specification = [ ('NUMBER', r'\d+(\.\d*)?'), # Integer or decimal number ('ASSIGN', r'='), # Assignment operator ('SEMI', r';'), # Statement terminator ('ID', r'[A-Za-z]+'), # Identifiers and keywords ('NEWLINE', r'\n'), # Line endings ('SKIP', r'[ \t]+'), # Skip over spaces and tabs ('MISMATCH', r'.'), # Any other character ] tok_regex = '|'.join('(?P<%s>%s)' % pair for pair in tokens_specification) get_token = re.compile(tok_regex).match line_num = 1 position = line_start = 0 mo = get_token(source_code, position) while mo is not None: type_ = mo.lastgroup if type_ == 'NEWLINE': line_start = position line_num += 1 elif type_ != 'SKIP': value = mo.group(type_) yield dict(token_type=type_, token_value=value, line=line_num, column=mo.start()-line_start) position = mo.end() mo = get_token(source_code, position) source_code_example = "x = 3; y = x + 5;" for token_info in lexer(source_code_example): print(f"{token_info['token_type']:<8} {token_info['token_value']} " f"(Line:{token_info['line']}, Col:{token_info['column']})") ``` 上述代码展示了如何创建一个基础的词法解析函数,它会遍历给定字符串并返回一系列字典对象表示找到的令牌信息。 #### LL(1)文法验证 编写一段程序用于检测给定的上下文无关文法(CFG)是否满足LL(1)条件。这涉及到计算FIRST集和FOLLOW集,并检查是否存在左递归或冲突的选择项等问题[^2]。 考虑如下伪码描述的过程: ```pseudo function Is_LL1(Grammar G){ foreach production p in G.productions do { Calculate FIRST(p.right_side); If there's any direct/indirect left recursion then return false; } For each non-terminal N{ Initialize FOLLOW(N); While changes occur to FOLLOW sets of all symbols{ Update FOLLOW based on productions rules. } } Check for conflicts within the parsing table constructed using FIRST & FOLLOW values. } ``` 这段逻辑旨在确保所定义的语言结构适合于自顶向下的预测分析技术应用。 #### 中间代码生成实例 展示从高级语言到三地址码(TAC)这种中间表示形式的具体变换过程。例如,将C风格赋值表达式转化为TAC指令序列[^3]: 原始语句:`a = b * c + d / e`; 对应的三地址代码可能如下所示: ```assembly t1 := b * c t2 := d / e t3 := t1 + t2 a := t3 ``` 通过这种方式简化复杂的算术运算以便后续阶段进一步处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值