提交 d72d92b5 创建 作者: 赵鹏翀's avatar 赵鹏翀

make template

上级 f8b2f960
流水线 #35228 已失败 于阶段
用时 48 秒
image: "registry.cn-hangzhou.aliyuncs.com/engintime/ubuntu_16.04_program:latest"
stages:
- make
- code-analysis
- case1
- case2
- case3
variables:
TEACHERCHECK_REPO: "engintime/cp-lab/teachers-packet/Lab03.git"
before_script:
- flex -b -omain.c scan.txt
- make
.codecode-runner: &codecode-runner
tags:
- ubuntu-16.04
- short-job
.codecode-analysis-runner: &codecode-analysis-runner
tags:
- ubuntu-16.04
- short-job
- code-analysis
make:
stage: make
<<: *codecode-runner
script:
- gcc --version
- make
- execscore.sh 60
only:
- master
code-analysis:
stage: code-analysis
<<: *codecode-analysis-runner
script:
- make
- mkdir -p build
- cppcheck -v --force --enable=all --xml -I./ ./ 2> ./build/cppcheck-report.xml
- bash -c 'find ./ -regex ".*\.c\|.*\.h" | vera++ -S -s -d -c ./build/vera-report.xml'
- valgrind --xml=yes --xml-file=./build/valgrind-report.xml ./app > /dev/null
- sonar-scanner -Dsonar.projectKey=ProjectKey-$CI_PROJECT_ID -Dsonar.projectName=$CI_PROJECT_NAME -Dsonar.links.homepage=$CI_PROJECT_URL -Dsonar.host.url=$SONAR_URL -Dsonar.login=$SONAR_LOGIN -Dsonar.gitlab.commit_sha=$CI_COMMIT_SHA -Dsonar.gitlab.ref_name=$CI_COMMIT_REF_NAME -Dsonar.gitlab.project_id=$CI_PROJECT_ID -Dsonar.gitlab.url=$CODECODE_PROTOCOL$CODECODE_DOMAIN
allow_failure: true
only:
- master
case1:
stage: case1
<<: *codecode-runner
script:
- make
- ./app input1.txt > user_output1.txt
- diff output1.txt user_output1.txt -b -B -y -i -W 100
- execscore.sh 70
......@@ -47,10 +62,6 @@ teacher-check1:
- diff template/input1.txt input1.txt -b -B -y -i --suppress-common-lines
- diff template/output1.txt output1.txt -b -B -y -i --suppress-common-lines
- fileidentity.sh
- git clone ${CODECODE_PROTOCOL}gitlab-ci-token:${CI_JOB_TOKEN}@${CODECODE_DOMAIN}/${TEACHERCHECK_REPO} teacher_check
- ./app teacher_check/input1.txt > user_output1.txt
- diff teacher_check/output1.txt user_output1.txt -b -B -y -i -W 100
- extracase.sh
only:
- master
when: manual
......@@ -60,6 +71,7 @@ case2:
stage: case2
<<: *codecode-runner
script:
- make
- ./app input2.txt > user_output2.txt
- diff output2.txt user_output2.txt -b -B -y -i -W 100
- execscore.sh 80
......@@ -76,10 +88,6 @@ teacher-check2:
- diff template/input2.txt input2.txt -b -B -y -i --suppress-common-lines
- diff template/output2.txt output2.txt -b -B -y -i --suppress-common-lines
- fileidentity.sh
- git clone ${CODECODE_PROTOCOL}gitlab-ci-token:${CI_JOB_TOKEN}@${CODECODE_DOMAIN}/${TEACHERCHECK_REPO} teacher_check
- ./app teacher_check/input2.txt > user_output2.txt
- diff teacher_check/output2.txt user_output2.txt -b -B -y -i -W 100
- extracase.sh
only:
- master
when: manual
......@@ -89,6 +97,7 @@ case3:
stage: case3
<<: *codecode-runner
script:
- make
- ./app input3.txt > user_output3.txt
- diff output3.txt user_output3.txt -b -B -y -i -W 100
- execscore.sh 100
......@@ -105,10 +114,6 @@ teacher-check3:
- diff template/input3.txt input3.txt -b -B -y -i --suppress-common-lines
- diff template/output3.txt output3.txt -b -B -y -i --suppress-common-lines
- fileidentity.sh
- git clone ${CODECODE_PROTOCOL}gitlab-ci-token:${CI_JOB_TOKEN}@${CODECODE_DOMAIN}/${TEACHERCHECK_REPO} teacher_check
- ./app teacher_check/input3.txt > user_output3.txt
- diff teacher_check/output3.txt user_output3.txt -b -B -y -i -W 100
- extracase.sh
only:
- master
when: manual
......
删除文件
#ifndef _DEFINE_H_
#define _DEFINE_H_
typedef enum
{
// 文件结束
ENDFILE,
// 错误
ERROR,
// 关键字
IF, // if
THEN, // then
ELSE, // else
END, // end
REPEAT, // repeat
UNTIL, // until
READ, // read
WRITE, // write
// 标识符
ID,
// 无符号整数
NUM,
// 特殊符号
ASSIGN, // :=
EQ, // =
LT, // <
PLUS, // +
MINUS, // -
TIMES, // *
OVER, // /
LPAREN, // (
RPAREN, // )
SEMI, // ;
// 注释
COMMENT // {...}
}TokenType;
#endif // _DEFINE_H_
在此文件中写入要读取的数据。在本地默认读取此文件的内容。参见项目属性页(Alt+F7)的调试命令参数属性。
\ No newline at end of file
a b c d e ok 2 3 4 5 6 7
\ No newline at end of file
在此文件中写入要读取的数据
\ No newline at end of file
read x;
if 0 < x then
fact := 1;
repeat
fact := fact * x;
x := x - 1
until
x = 0;
write fact
end
\ No newline at end of file
在此文件中写入要读取的数据
\ No newline at end of file
read x; /* input an integer */
if 0 < x then /* don't compute if x <= 0 */
fact := 1;
repeat
fact := fact * x;
x := x - 1
until
x = 0;
write fact // output factorial of x
end
\ No newline at end of file
在此文件中写入要读取的数据
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
功能:
主函数
参数:
argc - argv 数组的长度,大小至少为 1,argc - 1 为命令行参数的数量。
argv - 字符串指针数组,数组长度为命令行参数个数 + 1。其中 argv[0] 固定指向当前
所执行的可执行文件的路径字符串,argv[1] 及其后面的指针指向各个命令行参数。
例如通过命令行输入 "C:\hello.exe -a -b" 后,main 函数的 argc 的值为 3,
argv[0] 指向字符串 "C:\hello.exe",argv[1] 指向字符串 "-a",argv[2] 指向字符串 "-b"。
返回值:
成功返回 0, 失败返回 1
*/
int main(int argc, char* argv[])
{
// 使用第一个参数输入待处理文件的名称,若没有输入此参数就报告错误
if(argc < 2)
{
printf("Usage: app.exe filename\n");
return 1;
}
// 打开待处理的文件。读取文件中的内容。
FILE* file = fopen(argv[1], "rt");
if(NULL == file)
{
printf("Can not open file \"%s\".\n", argv[1]);
return 1;
}
// 使用合适的算法处理从文件中读取到的数据
// 使用 printf 函数将处理结果打印到标准输出。输出格式参考 output1.txt 文件。
// 关闭文件
fclose(file);
return 0;
}
if: 0
then: 0
else: 0
end: 0
repeat: 0
until: 0
read: 0
write: 0
id: 6
num: 6
assign: 0
eq: 0
lt: 0
plus: 0
minus: 0
times: 0
over: 0
lparen: 0
rparen: 0
semi: 0
comment: 0
error: 0
line: 0
在此文件中写入打印输出的内容
\ No newline at end of file
if: 1
then: 1
else: 0
end: 1
repeat: 1
until: 1
read: 1
write: 1
id: 10
num: 4
assign: 3
eq: 1
lt: 1
plus: 0
minus: 1
times: 1
over: 0
lparen: 0
rparen: 0
semi: 4
comment: 0
error: 0
line: 9
在此文件中写入打印输出的内容
\ No newline at end of file
if: 1
then: 1
else: 0
end: 1
repeat: 1
until: 1
read: 1
write: 1
id: 10
num: 4
assign: 3
eq: 1
lt: 1
plus: 0
minus: 1
times: 1
over: 0
lparen: 0
rparen: 0
semi: 4
comment: 3
error: 0
line: 9
在此文件中写入打印输出的内容
\ No newline at end of file
<?xml version="1.0" encoding="gb2312"?>
<OSLProject Version="1.00" Name="LEX" SubjectID="11c951f4-9b13-40e1-8b73-39ba7d73b89b" ProjectTemplateID="6fdce38e-9162-4060-b427-666eb3e3556b">
<OSLProject Version="1.00" Name="project" SubjectID="11c951f4-9b13-40e1-8b73-39ba7d73b89b" ProjectTemplateID="6fdce38e-9162-4060-b427-666eb3e3556b">
<Configurations>
<Configuration Name="Debug" CommandArgument="sample.txt">
<Configuration Name="Debug" CommandArgument="input.txt">
<Tool Name="PreBuildEventTool"/>
<Tool Name="CustomBuildTool"/>
<Tool Name="GCCCompilerTool" PreprocessorDefinitions="_DEBUG" GenerateDebugInformation="-1"/>
<Tool Name="GCCCompilerTool" PreprocessorDefinitions="_DEBUG" GenerateDebugInformation="-1" AdditionalOptions="-std=c99"/>
<Tool Name="PreLinkEventTool"/>
<Tool Name="GCCLinkerTool" AdditionalLibraries="fl"/>
<Tool Name="GCCLinkerTool" AdditionalLibraries=""/>
<Tool Name="PostBuildEventTool"/>
</Configuration>
<Configuration Name="Release" CommandArgument="sample.txt">
<Configuration Name="Release" CommandArgument="input.txt">
<Tool Name="PreBuildEventTool"/>
<Tool Name="CustomBuildTool"/>
<Tool Name="GCCCompilerTool" PreprocessorDefinitions="NDEBUG"/>
<Tool Name="GCCCompilerTool" PreprocessorDefinitions="NDEBUG" AdditionalOptions="-std=c99"/>
<Tool Name="PreLinkEventTool"/>
<Tool Name="GCCLinkerTool" AdditionalLibraries="fl"/>
<Tool Name="GCCLinkerTool" AdditionalLibraries=""/>
<Tool Name="PostBuildEventTool"/>
</Configuration>
</Configurations>
<Files>
<Filter Name="Lex输入文件">
<File RelativePath=".\scan.txt">
<FileConfiguration Name="Debug">
<Tool Name="CustomBuildTool" CommandLine="flex.exe -b -omain.c &quot;$(InputPath)&quot;&#xA;" Description="正在使用 Lex 生成扫描程序..." Outputs="lex.backup" AdditionalDependencies=""/>
</FileConfiguration>
<FileConfiguration Name="Release">
<Tool Name="CustomBuildTool" CommandLine="flex.exe -b -omain.c &quot;$(InputPath)&quot;&#xA;" Description="正在使用 Lex 生成扫描程序..." Outputs="lex.backup" AdditionalDependencies=""/>
</FileConfiguration>
</File>
</Filter>
<Filter Name="TINY语言样例">
<File RelativePath=".\sample.txt">
</File>
</Filter>
<Filter Name="头文件">
<File RelativePath=".\define.h">
</File>
<Filter Name="ͷļ" Filter="h;hpp;hxx">
</Filter>
<Filter Name="Դļ" Filter="cpp;c;cc;cxx">
<File RelativePath=".\main.c">
</File>
</Filter>
<File RelativePath=".\input.txt">
</File>
<File RelativePath=".\input1.txt">
</File>
<File RelativePath=".\input2.txt">
......
添加文件
{ Sample program
int TINY language -
computes factorial
}
read x; { input an integer }
if 0 < x then { don't compute if x <= 0 }
fact := 1;
repeat
fact := fact * x;
x := x - 1
until
x = 0;
write fact { output factorial of x }
end
%{
#include <stdio.h>
#include "define.h"
int lineno = 0; // 行数
// 符号计数器
int error_no = 0;
int if_no = 0;
int then_no = 0;
int else_no = 0;
int end_no = 0;
int repeat_no = 0;
int until_no = 0;
int read_no = 0;
int write_no = 0;
int id_no = 0;
int num_no = 0;
int assign_no = 0;
int eq_no = 0;
int lt_no = 0;
int plus_no = 0;
int minus_no = 0;
int times_no = 0;
int over_no = 0;
int lparen_no = 0;
int rparen_no = 0;
int semi_no = 0;
int comment_no = 0;
%}
newline \n
whitespace [ \t]+
%%
":=" { return ASSIGN; }
"=" { return EQ; }
"<" { return LT; }
"+" { return PLUS; }
"-" { return MINUS; }
"*" { return TIMES; }
"/" { return OVER; }
"(" { return LPAREN; }
")" { return RPAREN; }
";" { return SEMI; }
{newline} { lineno++; }
{whitespace} { /* 忽略空白 */ }
"{" {
// 匹配注释 {...}
char c;
int comment = 1;
do
{
c = input();
if(c == EOF)
{
comment = 0;
break;
}
else if(c == '\n')
lineno++;
else if(c == '}')
break;
}while(1);
return comment ? COMMENT : ERROR;
}
. { return ERROR; }
%%
TokenType id2keyword(const char* token);
void stat(TokenType tt, const char* token);
void output();
/*
功能:
主函数
参数:
argc - argv 数组的长度,大小至少为 1,argc - 1 为命令行参数的数量。
argv - 字符串指针数组,数组长度为命令行参数个数 + 1。其中 argv[0] 固定指向当前
所执行的可执行文件的路径字符串,argv[1] 及其后面的指针指向各个命令行参数。
例如通过命令行输入 "C:\hello.exe -a -b" 后,main 函数的 argc 的值为 3,
argv[0] 指向字符串 "C:\hello.exe",argv[1] 指向字符串 "-a",argv[2] 指向字符串 "-b"。
返回值:
成功返回 0, 失败返回 1
*/
int main(int argc, char* argv[])
{
TokenType tt;
// 使用第一个参数输入待处理文件的名称,若没有没有输入此参数就报告错误
if(argc < 2)
{
printf("Usage: scan.exe filename.\n");
return 1;
}
// 打开待处理的文件
FILE* file = fopen(argv[1], "rt");
if(NULL == file)
{
printf("Can not open file \"%s\".\n", argv[1]);
return 1;
}
// 将打开的文件作为 lex 扫描程序的输入
yyin = file;
// 开始扫描,直到文件结束
while((tt = yylex()) != ENDFILE)
{
// 根据符号类型统计其数量
stat(tt, yytext);
}
// 输出统计结果
output();
// 关闭文件
fclose(file);
return 0;
}
// 定义关键字与其类型的映射关系
typedef struct _KeyWord_Entry
{
const char* word;
TokenType type;
}KeyWord_Entry;
static const KeyWord_Entry key_table[] =
{
{ "if", IF },
{ "then", THEN },
{ "else", ELSE },
{ "end", END },
{ "repeat", REPEAT },
{ "until", UNTIL },
{ "read", READ },
{ "write", WRITE }
};
/*
功能:
将标识符转换为对应的关键字类型
参数:
id - 标识符字符串指针。可能是一个关键字,也可能是用户定义的标识符。
返回值:
成功返回 0, 失败返回 1
*/
TokenType id2keyword(const char* id)
{
//
// TODO: 在此添加源代码
//
return ID;
}
/*
功能:
根据符号类型进行数量统计。
参数:
tt - 符号类型。
token - 符号字符串指针。当符号被识别为标识符时,需要判断其是否为一个关键字。
返回值:
*/
void stat(TokenType tt, const char* token)
{
if(ID == tt)
{
tt = id2keyword(token);
}
switch(tt)
{
case IF: // if
if_no++;
break;
case THEN: // then
then_no++;
break;
case ELSE: // else
else_no++;
break;
case END: // end
end_no++;
break;
case REPEAT: // repeat
repeat_no++;
break;
case UNTIL: // until
until_no++;
break;
case READ: // read
read_no++;
break;
case WRITE: // write
write_no++;
break;
case ID: // 标识符
id_no++;
break;
case NUM: // 无符号整数
num_no++;
break;
case ASSIGN: // :=
assign_no++;
break;
case EQ: // =
eq_no++;
break;
case LT: // <
lt_no++;
break;
case PLUS: // +
plus_no++;
break;
case MINUS: // -
minus_no++;
break;
case TIMES: // *
times_no++;
break;
case OVER: // /
over_no++;
break;
case LPAREN: // (
lparen_no++;
break;
case RPAREN: // )
rparen_no++;
break;
case SEMI: // ;
semi_no++;
break;
case COMMENT: // {...}
comment_no++;
break;
case ERROR: // 错误
error_no++;
break;
}
}
// 输出统计结果
void output()
{
printf("if: %d\n", if_no);
printf("then: %d\n", then_no);
printf("else: %d\n", else_no);
printf("end: %d\n", end_no);
printf("repeat: %d\n", repeat_no);
printf("until: %d\n", until_no);
printf("read: %d\n", read_no);
printf("write: %d\n", write_no);
printf("id: %d\n", id_no);
printf("num: %d\n", num_no);
printf("assign: %d\n", assign_no);
printf("eq: %d\n", eq_no);
printf("lt: %d\n", lt_no);
printf("plus: %d\n", plus_no);
printf("minus: %d\n", minus_no);
printf("times: %d\n", times_no);
printf("over: %d\n", over_no);
printf("lparen: %d\n", lparen_no);
printf("rparen: %d\n", rparen_no);
printf("semi: %d\n", semi_no);
printf("comment: %d\n", comment_no);
printf("error: %d\n", error_no);
printf("line: %d\n", lineno);
}
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论