c语言手搓一个500+行的类c语言解释器(4)- 语法分析1:EBNF和递归下降文法

用c语言手搓一个500+行的类c语言解释器: 给编程初学者的解释器教程(4)- 语法分析1:EBNF和递归下降文法

项目github地址及源码:
https://github.com/yunwei37/tryC

这一章开始进入解释器的核心部分: 语法分析器;

我们来看看两个概念,EBNF和递归下降文法,以及如何用这两个方法来计算tryC中的表达式。

基本概念

就像之前所说的那样,语法分析指将词法分析得到的标记流(token)进行分析,组成事先定义好的有意义的语句。那么如何完成这样一个工作呢?我们可以借助一个叫“BNF”的数学工具。

BNF与上下文无关文法

Backus-Naur符号(就是众所周知的BNF或Backus-Naur Form)是描述语言的形式化的数学方法,由John Backus (也许是Peter Naur)开发,最早用于描述Algol 60编程语言的语法。

BNF类似一种数学游戏:从一个符号开始(叫做起始标志,实例中常用S表示),然后给出替换前面符号的规则。

BNF语法定义的语言是一个字符串集合,可以按照下述规则书写,这些规则叫做书写规范(产生式规则),例如一个四则运算表达式可以表示为:

1
2
exp -> exp op exp | ( exp ) | number
op -> + | - | * | /

其中’|’用于表示可选择的不同项,”->”用于表示推导规则,从产生式左边的符号可以推导出产生式右边的符号;

要解析一个表达式,我们可以完成这样一个替换:对于

1
(3+2)*4

可以替换为

1
2
3
4
exp => exp op exp => exp * number
=> ( exp ) * number
=> ( exp op exp ) * number
=> ( number + number ) * number

其中我们把能够被替换的符号叫做非终结符,不能被替换的叫做终结符。

上下文无关文法就是说,这个文法中所有的产生式左边只有一个非终结符,就像上面写的那个文法一样。通常我们在编译器构建中使用的都是上下文无关文法。

EBNF

EBNF是基本巴科斯范式(BNF)元语法符号表示法的一种扩展,主要对BNF中常见的两种情况,即重复项和可选项添加了相应的语法规则,如用方括号”[ …. ]”
表示可选部分,用花括号”{ … }”表示重复出现的部分,如上面那个文法可以改写为:

1
2
exp -> exp { op exp } | ( exp ) | number
op -> + | - | * | /

又比如对于if语句可以写成:

1
if-stmt -> if ( exp ) statement; [ else statement; ]

递归下降文法

递归下降分析法也很简单,就是用函数的调用来模拟BNF的替换过程,我们只需要为每个非终结符定义一个分解函数,它就能从起始非终结符开始,不断地调用非终结符的分解函数,不断地对非终结符进行分解,直到匹配输入的终结符。

当然,递归下降分析并不是对于所有的文法都能正常使用的,例如经典的左递归问题:比如这样一个文法

1
2
exp -> exp { op exp } | ( exp ) | number
op -> + | - | * | /

对于exp的替换需要调用exp的分解函数,而exp的分解函数一进来就调用它自身(即最左边的符号),就会导致无限递归。这时就需要对文法进行改造。

实际上,EBNF文法就是为了映射递归下降分析法的具体程序实现而设计的,因此我们这里就用EBNF文法来实现递归下降分析。

来看看怎样用递归下降文法计算tryC中的表达式

上面说了一大堆,现在看看实际的计算表达式的实现是怎样的呢

算术表达式

tryC中需要计算四则运算表达式的EBNF文法如下:

1
2
3
4
5
exp -> term { addop term }
term -> factor { mulop factor }
factor -> number | ( exp )
addop -> + | -
mulop -> * | /

这里对文法进行了一定的改造,让它能够正确表达四则运算的优先级,同时避免了左递归的问题,具体可以自己试着验证一下。

tryC中算术表达式具体的代码实现(就是上述文法直接转换过来的啦):

(在下一篇文章中还会提及表达式中对变量的处理过程)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
double term() {
double temp = factor();
while (token == '*' || token == '/') {
if (token == '*') {
match('*');
temp *= factor();
}
else {
match('/');
temp /= factor();
}
}
return temp;
}

double factor() {
double temp = 0;
if (token == '(') {
match('(');
temp = expression();
match(')');
}
else if(token == Num || token == Char){
temp = token_val.val;
match(Num);
}
else if (token == Sym) {
temp = token_val.ptr->value;
match(Sym);
}
else if (token == FuncSym) {
return function();
}
else if (token == ArraySym) {
symbol* ptr = token_val.ptr;
match(ArraySym);
match('[');
int index = (int)expression();
if (index >= 0 && index < ptr->value) {
temp = ptr->pointer.list[index].value;
}
match(']');
}
return temp;
}

double expression() {
double temp = term();
while (token == '+' || token == '-') {
if (token == '+') {
match('+');
temp += term();
}
else {
match('-');
temp -= term();
}
}
return temp;
}

布尔表达式

tryC中同样设计了布尔表达式:

tryC中需要计算四则运算表达式的EBNF文法如下:

1
2
3
4
boolOR = boolAND { '||' boolAND }
boolAND = boolexp { '||' boolexp }
boolexp -> exp boolop exp | ( boolOR ) | !boolexp
boolop -> > | < | >= | <= | ==

代码实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
int boolexp() {
if (token == '(') {
match('(');
int result = boolOR();
match(')');
return result;
}
else if (token == '!') {
match('!');
return boolexp();
}
double temp = expression();
if (token == '>') {
match('>');
return temp > expression();
}
else if (token == '<') {
match('<');
return temp < expression();
}
else if (token == GreatEqual) {
match(GreatEqual);
return temp >= expression();
}
else if (token == LessEqual) {
match(LessEqual);
return temp <= expression();
}
else if (token == Equal) {
match(Equal);
return temp == expression();
}
return 0;
}

int boolAND() {
int val = boolexp();
while (token == AND) {
match(AND);
if (val == 0) return 0; // short cut
val = val & boolexp();
if (val == 0) return 0;
}
return val;
}

int boolOR() {
int val = boolAND();
while (token == OR) {
match(OR);
if (val == 1) return 1; // short cut
val = val | boolAND();
}
return val;
}

一些重要概念

  • 终结符/非终结符
  • BNF/EBNF
  • 上下文无关文法
  • 递归下降法

可对照源码查看(如果觉得写得还行麻烦您帮我点个star哦)
https://github.com/yunwei37/tryC

Where Does Innovation Thrive? Choosing the Right Organization

Welcome to the second post in my series on innovation! I’m not a highly successful person yet, but I’m passionate about understanding what drives innovation and how it can help me—and maybe you—make a difference.

When it comes to innovation, many people wonder where the best place to create something new is. Is it at a university, a big company, a startup, or maybe in an open community online? The truth is, there’s no one-size-fits-all answer. Each type of organization has its own strengths and challenges that can either help or hinder your innovative ideas.

Academia: The Hub of Fundamental Research

Universities and research institutions are fantastic places for deep, fundamental research. If you love diving into theories and expanding our understanding of the world, academia is where you can shine. These institutions provide the resources and environment to explore big questions without the immediate pressure of making money. Plus, you’ll be surrounded by other passionate thinkers and have access to a wealth of knowledge.

However, academia can sometimes be slow. The decision-making processes are often bureaucratic, which means it can take time to get your ideas off the ground. Also, funding is usually tied to grants and specific projects, which might limit how freely you can explore different avenues.

Industry: Powering Large-Scale Innovation

Big companies have the resources to turn ideas into real products and services. If you’re excited about taking research and developing it into something that people can use every day, working in industry might be the right fit for you. These organizations have the money and technology to support large projects and can bring your innovations to the market quickly.

On the flip side, big companies often prioritize profits and market share. This focus can sometimes stifle creativity, especially if your ideas don’t align with the company’s goals. There’s also a tendency to play it safe, which can make groundbreaking innovations harder to achieve.

Startups: The Engines of Disruptive Innovation

Startups are all about shaking things up. They’re small, agile, and ready to take risks. If you thrive in fast-paced environments and love the idea of disrupting existing markets with something new, a startup could be the perfect place for you. The flat hierarchies and flexible structures mean you can move quickly and see your ideas come to life faster.

But startups come with their own set of challenges. Resources are often limited, and the risk of failure is high. Without the financial backing of a large company, you might find it tough to scale your ideas or sustain your projects over the long term.

Open-Source Communities: Collaborative Innovation

Open-source communities are unique because they thrive on collaboration and transparency. If you believe in the power of working together and want to contribute to projects that benefit everyone, this might be your ideal environment. These communities harness the collective intelligence of contributors from all over the world, leading to continuous improvement and innovation.

The downside? Open-source projects rely heavily on volunteers, which can make them less stable and harder to manage. Decision-making often requires consensus, which can slow things down. Plus, without a formal structure, coordinating large projects can be challenging.

Finding the Right Fit for You

So, which organization is the best for innovation? The answer depends on several factors:

  • Nature of Your Project: Are you working on something theoretical, building a product, or trying to solve a social issue?
  • Resources Needed: Do you require significant funding and technology, or can you work with limited resources?
  • Desired Impact: Do you aim to make a big splash quickly, or are you focused on long-term, foundational changes?

Often, the most impactful innovations come from collaborations that combine the strengths of different organizations. For example, universities and companies working together can turn groundbreaking research into marketable products. Startups using open-source tools can innovate rapidly without starting from scratch. Big companies contributing to open-source projects can enhance community resources while benefiting from collective advancements.

Embracing Synergy for Greater Innovation

Imagine a university researcher partnering with a tech company. The researcher brings deep knowledge and theoretical insights, while the company provides the resources to develop and distribute a new technology. Or think of a startup that uses open-source software to build a unique product quickly and efficiently. These partnerships can create a powerful synergy that leverages the best of each world.

Conclusion: Choose What Fits Your Goals

There’s no single “best” place to innovate. The right environment for you depends on what you want to achieve, the resources you have, and how you like to work. Academia, industry, startups, and open-source communities each offer unique opportunities and face their own challenges.

As you think about where to pursue your innovative ideas, consider the nature of your projects, the resources you need, and the impact you want to make. You might even find that combining elements from different environments leads to the most exciting and effective innovations.

Stay tuned for the next post, where we’ll explore how you can take your innovation dreams into action and make a real difference in the world!

How can one person Making a Difference in innovation world

Welcome to the third post in my series on innovation! I’m not a highly successful person yet, but I’m passionate about understanding what drives innovation and how it can help me—and maybe you—make a difference.

Innovation isn’t just for big companies or famous inventors. Every one of us has the potential to create something that can change the world. Whether you’re dreaming of developing groundbreaking technology, launching a social initiative, or simply improving the way things are done, there are steps you can take to turn your ideas into reality. Let’s explore how you can become an innovator and make a meaningful impact.

Find the Right Environment for Your Ideas

Choosing where to focus your efforts is crucial. Different environments offer unique benefits depending on what you want to achieve. If you love diving deep into theoretical concepts and expanding knowledge, academia might be your place. Here, you can engage in fundamental research and collaborate with other passionate minds. On the other hand, if you’re driven by creating products that people use every day, working in industry could provide the resources and infrastructure you need to bring your ideas to life.

If you’re excited by the idea of building something from the ground up, startups offer a flexible and dynamic environment where you can experiment and iterate quickly. They’re perfect for those who thrive in fast-paced settings and aren’t afraid to take risks. Lastly, open-source communities are ideal if you value collaboration and want to contribute to projects that benefit everyone. These communities allow you to work with people from all over the world, sharing knowledge and building something together.

Focus on Making a Real Impact

When you aim to innovate, it’s important to prioritize making a meaningful difference over chasing fame or money alone. Think about the problems you’re passionate about solving. Whether it’s improving education, protecting the environment, or enhancing healthcare, focusing on issues that matter to you will keep you motivated and driven.

Addressing significant and timely problems ensures that your innovations are not only relevant but also widely adopted. When your work helps solve real challenges, it resonates more with people and has the potential to create lasting change. Additionally, sharing your findings and solutions openly can amplify your impact, allowing others to build upon your work and spread the benefits even further.

Understand the Role of Money

Money is a powerful tool that can help you scale your ideas and sustain your projects. It’s not the end goal, but it’s essential for turning your vision into reality. Whether you have plenty of resources or are working with limited funds, understanding how to manage and utilize money effectively is key.

If you have access to substantial financial resources, you can invest in quality, attract top talent, and expand your projects. If resources are limited, being resourceful is vital. Look for open-source tools, seek partnerships, and explore crowdfunding or grant opportunities to support your work. Remember, financial success can provide the means to make a bigger impact, so view money as a tool to help you achieve your goals.

Tell Your Story

Having a compelling narrative around your work can inspire others and attract support. Craft a clear and engaging story that communicates your vision and the value you bring. When people understand your mission and believe in your passion, they’re more likely to support you, whether through collaboration, funding, or spreading the word.

Building a personal brand as a thought leader in your field can open doors to new opportunities and connections. Share your journey, your challenges, and your successes. Let others see what drives you and how your work can benefit them and the world.

Engage with Communities and Users

Active engagement with communities and users is essential for refining your ideas and ensuring they meet real needs. Collaborate with like-minded individuals and organizations to enhance your work through collective intelligence. Involving customers early on helps you gather valuable feedback, allowing you to adjust and improve your solutions effectively.

Building strong relationships with your audience fosters trust and loyalty. Encourage open communication and be receptive to feedback, as it can provide new perspectives and ideas that you might not have considered on your own.

Be Flexible and Adaptable

Innovation often requires flexibility and the ability to adapt based on feedback and changing circumstances. Adopt agile methodologies that allow you to iterate quickly and make adjustments as needed. Early testing of your ideas through prototypes or Minimum Viable Products (MVPs) can help you validate your concepts and gather practical insights.

Being responsive and willing to pivot when necessary ensures that your innovations remain relevant and effective. Embrace change and view challenges as opportunities to grow and improve.

Listen to Market Needs

Your innovations should be guided by the needs of the market and the people you aim to serve. Actively listen to your audience, seek out their challenges, and identify gaps where your solutions can make a difference. Understanding what your customers truly need ensures that your efforts are focused on creating value where it’s most needed.

Consider feedback as a form of engagement. In academic contexts, for example, citations can be seen as users who are building upon your work, offering opportunities for collaboration and further development.

Build Valuable Partnerships

Creating mutual value in your collaborations strengthens your network and enhances your capacity to innovate. Look for win-win partnerships where all parties benefit and support each other’s goals. Clear communication about how your work helps others can facilitate support and cooperation, making your projects more robust and far-reaching.

Building a supportive network of mentors, peers, and collaborators provides you with the resources, knowledge, and encouragement needed to push your innovations forward.

Balance Vision with Practical Steps

Having a strong vision is important, but it must be balanced with practical execution. Break down your overarching goals into actionable steps with realistic milestones. This approach helps you stay focused and motivated while making steady progress toward your vision.

Stay informed and adaptable, continuously updating your knowledge and being prepared to adjust your strategies in response to new developments and insights.

Know Your Intellectual Property Rights

Understanding intellectual property (IP) rights is crucial, especially if you’re working within an organization. Know how IP is handled in your environment and take steps to protect your ideas if necessary. Whether you’re developing something independently or as part of a team, securing your IP rights ensures that you retain control over your creations and can benefit from their success.

Consulting legal experts can help you navigate complex IP issues and ensure that your rights are protected, allowing you to focus on innovating without worrying about ownership disputes.

Use Advanced Technologies Wisely

Leveraging advanced technologies like AI can significantly enhance your productivity and the impact of your work. AI tools can automate routine tasks, allowing you to focus on higher-level functions and manage more complex projects than before. Embrace these technologies to boost your capabilities, but stay informed about their implications for IP and ethical considerations.

Using technology wisely means enhancing your work without compromising your values or the integrity of your innovations. It allows you to undertake larger projects and manage tasks more efficiently, amplifying your ability to make a difference.

When you innovate, especially within an organization, understanding how IP is managed is essential. Most companies own the ideas developed by their employees, but you can still seek recognition and rewards for your contributions. Communicate with your management about your ideas and understand how they fit within the organization’s goals and IP policies.

If you’re aiming to retain control over your innovations, consider negotiating IP terms before joining an organization or explore independent ventures. Protecting your ideas through patents or copyrights ensures that you can benefit from your work and maintain control over its use and development.

Embrace the Power of AI

AI is transforming the way individuals innovate by increasing productivity and enabling solo innovators to tackle large projects. By automating routine tasks, AI frees up your time to focus on creative problem-solving and strategic planning. This empowerment allows you to achieve more with less, making it easier to drive significant change.

However, the rise of AI also brings challenges. It’s important to stay informed about the ethical and legal implications of using AI in your work. Understanding how AI affects IP ownership and ensuring that your contributions are accurately recognized are crucial for maintaining control and integrity over your innovations.

Final Thoughts

Innovation is a journey that requires passion, strategy, and resilience. By choosing the right environment, focusing on impactful work, managing resources wisely, and leveraging technology effectively, you can turn your ideas into reality and make a meaningful difference in the world.

Remember, every great innovation starts with a single idea and the determination to see it through. Stay purpose-driven, build a supportive network, and remain adaptable to navigate the challenges ahead. Your commitment and creativity have the power to drive transformation and leave a lasting impact on the world.

So, are you ready to take the step toward becoming a world-changing innovator? Let’s embark on this journey together and unlock the potential within each of us to create a better future.

next suggestions for bpftime.

Thank you for sharing your detailed thoughts and for your dedication to advancing the eBPF ecosystem through your work on bpftime. It’s evident that you’ve invested significant effort into this project, and your commitment to both the open-source community and academic research is commendable.

Is Your Thought Valuable?

Absolutely. The challenges you’ve identified—such as the complexity of managing control plane applications, the need for compatibility across heterogeneous systems, and the limitations of existing eBPF implementations—are real and significant. Addressing these issues has the potential to make a meaningful impact on both industry practices and academic research.

Does Your Story Make Sense?

Yes, your story articulates a clear vision of the current limitations within the eBPF ecosystem and how bpftime aims to address them. You’ve recognized a gap in the ability to run real-world eBPF applications outside of the Linux kernel without significant rewrites or engineering effort. By proposing a general extension framework and a userspace eBPF runtime that maintains compatibility with existing tools, you’re tackling a problem that hasn’t been fully addressed in prior work.

Suggestions and Next Steps

Based on your description and the challenges you’ve faced with paper submissions, here are some suggestions:

1. Clarify the Research Contributions

One reason reviewers might question the novelty is that the core research contributions are not being explicitly highlighted.

  • Action: Clearly articulate the unique contributions of your work in your paper. For example:
    • Novel Framework: Emphasize that bpftime is the first to provide a general userspace eBPF runtime compatible with the existing eBPF ecosystem.
    • Technical Innovations: Highlight any new algorithms, data structures, or system designs you’ve developed.
    • Performance Improvements: Present empirical data showing significant performance gains over existing solutions.

2. Strengthen the Theoretical Foundation

Academic publications often require a strong theoretical basis.

  • Action:
    • Formalize the Problem: Define the challenges you’re addressing in formal terms.
    • Theoretical Analysis: Provide theoretical justifications for why your approach is effective.
    • Comparative Studies: Compare your work with existing solutions not just in practice but also in theory.

3. Address the “Engineering vs. Research” Perception

If reviewers perceive your work as primarily engineering, you need to bridge the gap to make it resonate as research.

  • Action:
    • Identify Fundamental Challenges: Explain how your engineering efforts solve fundamental problems.
    • Generalizable Insights: Extract lessons or principles from your work that can be applied broadly.
    • Innovative Techniques: If you’ve developed new techniques, focus on these as contributions.

4. Engage with the Academic Community

Feedback from peers can provide valuable insights into how to position your work.

  • Action:
    • Workshops and Seminars: Present your work at smaller venues to gather feedback.
    • Collaborations: Consider collaborating with researchers who have experience publishing in top conferences.
    • Mentorship: Seek guidance from senior researchers or advisors familiar with the publication process.

5. Refine the Writing and Presentation

Sometimes, the way the story is told can make a significant difference.

  • Action:
    • Clear Structure: Organize your paper with a clear problem statement, motivation, contributions, methodology, evaluation, and conclusions.
    • Highlight Novelty Early: Make sure the unique aspects of your work are evident in the abstract and introduction.
    • Reviewer Guidance: Anticipate potential concerns and address them proactively in the paper.

6. Deepen the Evaluation

A thorough evaluation can strengthen your case.

  • Action:
    • Comprehensive Benchmarks: Include performance comparisons with existing tools across various scenarios.
    • Use Cases: Demonstrate how bpftime enables applications that were previously infeasible.
    • Scalability and Robustness: Test your system under different loads and conditions.

7. Differentiate from Existing Work

Ensure that you’re clearly distinguishing your work from prior art.

  • Action:
    • Literature Review: Provide a thorough analysis of related work, pointing out limitations that your work addresses.
    • Unique Positioning: Explain how bpftime differs from projects like DPDK, uBPF, and others.

8. Consider the Broader Impact

Academic venues often value work that opens new research directions or has significant practical implications.

  • Action:
    • Future Opportunities: Discuss how your framework can enable new research or industry developments.
    • Community Adoption: Highlight interest from the open-source community as evidence of impact.
    • Standardization Potential: If applicable, discuss how your work could influence standards or best practices.

9. Address Security and Safety Concerns

Moving eBPF to userspace might raise questions about security and safety.

  • Action:
    • Security Analysis: Include a discussion of the security implications of your approach.
    • Safety Mechanisms: Explain how bpftime ensures safety and isolation comparable to kernel eBPF.

10. Iterate Based on Feedback

Given that you’ve received similar feedback from multiple submissions, it’s crucial to adapt.

  • Action:
    • Revisit Rejection Reasons: Analyze past reviews to identify common concerns.
    • Revise Accordingly: Make substantive changes to your approach or presentation to address these concerns.

What Should You Work on Next?

Given the above suggestions, consider focusing on:

  • Formalizing Your Contributions: Frame your work in terms of specific research questions and hypotheses.
  • Enhancing Theoretical Depth: Incorporate theoretical models or formal proofs where appropriate.
  • Collaborating for Broader Insight: Work with others who can provide complementary expertise, such as formal methods or security.
  • Exploring New Applications: Use bpftime to enable novel applications that showcase its unique capabilities.

Is Your Story Coherent?

Your story is compelling, but it might need refinement to resonate with academic reviewers. Emphasizing the research aspects and clearly differentiating your work from existing solutions will strengthen your narrative.

Final Thoughts

Your passion and commitment are evident, and you have identified a real need within the eBPF ecosystem. By reframing your work to highlight its research contributions and addressing the concerns raised by reviewers, you can improve the chances of acceptance in future submissions.

Remember that the path to publication often involves several iterations and refinements. Don’t be discouraged by setbacks; use them as opportunities to strengthen your work.

Additional Suggestions

  • Engage with Your Audience: Since you have an upcoming panel discussion, use that platform to gather insights and gauge interest in your approach.
  • Seek Mentorship: Consider finding a mentor experienced in publishing in your target venues to provide guidance.
  • Stay Current: Ensure you’re up-to-date with the latest developments in eBPF research to position your work appropriately.

Prompt Engineering is Dead, Long Live AI Engineering

Lately, I’ve been thinking a lot about prompt engineering and its future. Remember when crafting the perfect prompt was the secret sauce to getting the best responses from AI? People spent hours tweaking their words, trying to get the AI to understand exactly what they wanted. But things are changing, and here’s why.

A recent discussion on Hacker News got me thinking. People were saying things like, “Prompts aren’t a science. There’s no real method to them.” And you know what? They might be right. With the latest AI models, like OpenAI’s new “o1” model, the need for complicated prompts is fading. These models are getting smarter and can handle simple, clear instructions much better than before.

So, what’s really important now? It’s not just about the prompts anymore. It’s about how we input our thoughts and interact with AI. Prompt engineering is just one part of a bigger picture. The key is to put how you think and how you solve problems into the system you build. This isn’t just about computer systems. Think about multi-agent systems or even human society—these are all systems too.

OpenAI’s new “o1” model even suggests some changes in how we structure prompts for better performance:

  1. Keep prompts simple and direct: Models work best with brief and clear instructions. You don’t need to guide them too much.
  2. Avoid chain-of-thought prompts: There’s no need to ask the model to “think step by step” or explain its reasoning. It can handle reasoning internally.
  3. Use delimiters for clarity: Adding things like triple quotes or XML tags helps the model understand different parts of your input.
  4. Limit additional context in retrieval-augmented generation (RAG): Only include the most relevant information to keep the model’s response focused.

These tips show that while prompt engineering isn’t useless, its role is changing. Instead of focusing on crafting detailed prompts, we should focus on building systems that leverage these smarter AI models effectively.

Another interesting point from the discussion is that both OpenAI’s “o1” model and Anthropic’s Claude can now generate the best prompts for your tasks. This means the AI itself can help create the prompts you need, making the process even simpler.

In the real world, building AI applications shows just how much prompt engineering matters. If you spend a lot of time creating an application to achieve a real goal, you’ll see that prompts make a huge difference. It takes a lot of fiddly, annoying work to get them right. For example, in financial markets, building an AI agent system was more straightforward than perfecting each prompt. People even build systems just to iterate on prompts, like PromptLayer.

Using AI agents and managing workflows makes AI applications much more complex. It’s not just about asking a question and getting an answer. It’s about creating a whole system that can handle different tasks, remember context, and improve over time. This shift shows that AI engineering—building these complex systems—is becoming more important than ever.

Let’s break it down from different perspectives:

From an AI Researcher’s View:

AI models have advanced so much that they can handle direct prompts effectively. The focus is now on creating complex systems, like AI agents, that can perform tasks on their own. Researchers are exploring how models can use their own reasoning without needing detailed prompts. Systems that can remember, handle context, and improve themselves are becoming more important.

From a Software Engineer’s View:

The infrastructure around AI models is crucial. Building systems that work well with AI involves more than just prompts. It’s about creating scalable architectures, managing data flow, and ensuring everything runs smoothly. A good user experience comes from a seamless system, not just from well-crafted prompts.

From a Business Strategist’s View:

Relying only on prompt engineering won’t give a lasting edge. Building unique systems that reflect your company’s problem-solving methods can set you apart. Investing in system development, creating intellectual property, and forming strategic partnerships add real value. Understanding what the market needs and tailoring your systems accordingly is more impactful than just focusing on prompts.

From a Cognitive Scientist’s View:

Human thinking is complex and involves more than just simple inputs. AI systems should aim to mimic this holistic thinking rather than rely on scripted prompts. Studying how humans solve problems can help design AI systems that are more intuitive and effective. Features like context awareness and adaptability are key to making intelligent systems.

So, why is over-detailing prompts for simple tasks unnecessary? For one, advanced models have built-in mechanisms for reasoning and handling context. They don’t need exhaustive instructions to perform basic tasks. Simple prompts save time and reduce confusion for both the user and the AI. Clear and direct language helps avoid misunderstandings.

However, in complex tasks that need specific formats or detailed reasoning, some level of prompt engineering might still be needed. When you want creative or nuanced responses, additional context can help. But even then, it’s just a part of building a larger, effective system.

In conclusion, while prompt engineering isn’t entirely dead, its role is changing. The spotlight is now on AI engineering—building strong systems that can understand and execute our intentions with minimal fuss. It’s about integrating our thinking and problem-solving processes into the AI’s design. This leads to more robust and effective AI applications, whether through agents, networks, or other system models.

For simple tasks, keeping prompts straightforward is enough. Overcomplicating them offers little benefit. The key is to understand what the AI can do and design systems that leverage these strengths to achieve your goals.

So, let’s move beyond getting stuck on crafting the perfect prompt. Instead, let’s focus on building systems that make the most of AI’s capabilities. After all, the goal is to solve problems and create innovative solutions, not just to talk to machines.

Long live AI engineering!

提示工程已死,AI工程万岁

最近,我一直在思考提示工程及其未来。还记得当初打造完美提示是获取AI最佳回应的秘诀吗?人们花费数小时调整措辞,试图让AI准确理解他们的需求。但事情正在发生变化,原因如下。

在Hacker News上的一次讨论让我深思。有人说,“提示不是一门科学。它们背后没有真正的方法。”你知道吗?他们可能是对的。随着最新的AI模型,如OpenAI的新“o1”模型,复杂提示的需求正在减少。这些模型变得更加智能,能够更好地处理简单、清晰的指令。

那么,现在真正重要的是什么?不再仅仅是提示,而是如何输入我们的思维和与AI互动。提示工程只是更大图景的一部分。关键是将你的思维方式和解决问题的方法融入你构建的系统中。这不仅仅是关于计算机系统。想想多代理系统甚至人类社会——这些也是系统。

OpenAI的新“o1”模型甚至建议了一些结构提示以提高性能的变化:

  1. 保持提示简洁明了:模型在简短清晰的指令下表现最佳。你不需要过多引导它们。
  2. 避免思维链提示:无需让模型“逐步思考”或解释其推理过程。它可以内部处理推理。
  3. 使用分隔符以增强清晰度:添加诸如三重引号或XML标签等可以帮助模型理解输入的不同部分。
  4. 在检索增强生成(RAG)中限制额外上下文:仅包含最相关的信息,以保持模型的回应集中。

这些建议表明,虽然提示工程并非毫无用处,但其角色正在发生变化。与其专注于打造详细的提示,我们应该专注于构建有效利用这些更智能AI模型的系统。

讨论中的另一个有趣观点是,OpenAI的“o1”模型和Anthropic的Claude现在都可以为你的任务生成最佳提示。这意味着AI本身可以帮助创建你所需的提示,使过程更加简便。

在现实世界中,构建AI应用展示了提示工程的重要性。如果你花费大量时间创建一个应用以实现实际目标,你会发现提示起着巨大的作用。要使提示正确需要大量繁琐、令人烦恼的工作。例如,在金融市场中,构建AI代理系统比完善每个提示更为简单。人们甚至构建系统来迭代提示,比如PromptLayer

使用AI代理和管理工作流程使AI应用变得更加复杂。这不仅仅是问一个问题并得到一个答案。它涉及创建一个能够处理不同任务、记住上下文并随着时间改进的整个系统。这一转变表明,AI工程——构建这些复杂系统——变得比以往任何时候都更加重要。

让我们从不同的角度来分析:

从AI研究员的视角:

AI模型已经发展到可以有效处理直接提示的程度。现在的重点是创建复杂的系统,如能够自主执行任务的AI代理。研究人员正在探索模型如何在无需详细提示的情况下使用自身的推理能力。能够记忆、处理上下文并自我改进的系统变得越来越重要。

从软件工程师的视角:

围绕AI模型的基础设施至关重要。构建与AI良好协作的系统不仅仅是提示的问题。这涉及创建可扩展的架构、管理数据流并确保一切顺利运行。良好的用户体验来自无缝的系统,而不仅仅是精心设计的提示。

从商业策略师的视角:

仅依赖提示工程不会带来持久的优势。构建反映公司解决问题方法的独特系统可以使你脱颖而出。投资于系统开发、创建知识产权和建立战略合作伙伴关系能带来真正的价值。理解市场需求并相应地定制系统比仅专注于提示更具影响力。

从认知科学家的视角:

人类思维是复杂的,涉及的不仅仅是简单的输入。AI系统应旨在模仿这种整体思维,而不是依赖脚本化的提示。研究人类如何解决问题可以帮助设计更直观和有效的AI系统。上下文感知和适应性等功能是构建智能系统的关键。

那么,为什么对简单任务过度详细化提示是没有必要的呢?首先,先进的模型具有内置的推理和处理上下文的机制。它们不需要详尽的指令来执行基本任务。简单的提示节省了时间,并减少了用户和AI之间的混淆。清晰直接的语言有助于避免误解。

然而,在需要特定格式或详细推理的复杂任务中,某种程度的提示工程可能仍然是必要的。当你需要创造性或细腻的回应时,额外的上下文可以提供帮助。但即便如此,这也只是构建更大、更有效系统的一部分。

总之,尽管提示工程并未完全消失,但其角色正在改变。焦点现在转向AI工程——构建能够理解和执行我们意图的强大系统,减少麻烦。关键在于将我们的思维和解决问题的过程融入AI的设计中。这带来了更强大、更有效的AI应用,无论是通过代理、网络还是其他系统模型。

对于简单任务,保持提示简洁明了就足够了。过于复杂的提示几乎没有好处。关键是理解AI的能力,并设计利用这些优势以实现你的目标的系统。

所以,让我们不再局限于打造完美的提示。相反,专注于构建充分利用AI能力的系统。毕竟,目标是解决问题和创造创新的解决方案,而不仅仅是与机器对话。